how do I get the reponse text from ajax / jquery?

后端 未结 3 692
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 04:45

Imagine I run this:

     $.ajax({
        type: \'POST\',
        url: \'/ajax/watch.php\',
        data: {\'watch\':\'aukcia\', \'id\':aukciaID},
        co         


        
3条回答
  •  天涯浪人
    2021-01-02 05:43

    Looks like somehow your jQuery is returning the XMLHttpRequest object, instead of your response.

    If that is the case, you should ask for its responseText property, like this:

     $.ajax({
        type: 'POST',
        url: '/ajax/watch.php',
        data: {'watch':'aukcia', 'id':aukciaID},
        complete: function(r){
           alert(r.responseText);
        }
     });
    

    However, if that does not work, you might be actually receiving a JSON response, and the [object Object] you are seeing might be your browser's representation of your JSON response.

    You should be able to inspect its contents by navigating around the object properties. However, if you want, you can also tell jQuery not to parse your JSON response, by including dataType: 'text' on your call:

     $.ajax({
        type: 'POST',
        url: '/ajax/watch.php',
        data: {'watch':'aukcia', 'id':aukciaID},
        dataType: 'text',
        complete: function(data){
           alert(data);
        }
     });
    

    For more information, see: http://api.jquery.com/jQuery.ajax/

提交回复
热议问题