How to get response text from ajax / jquery?

Imagine running this:

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

Inside / ajax / watch.php, let's say I have this:

echo 'this is what I want';

And a warning (responseText) appears:

[object Object]

Instead of my text string that I need. Any help please?

+6
source share
3 answers

It looks like somehow your jQuery is returning an XMLHttpRequest object instead of your answer.

If so, you should request its property responseText, for example:

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

However, if this does not work, you may get a JSON response, and the [object Object]one you see may be your representation of your browser JSON response.

, . , , jQuery JSON, dataType: 'text' :

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

. http://api.jquery.com/jQuery.ajax/

+16

ajax

 $.ajax({
        type: "POST",
        url: "insert-data.php",
        data: 
    {student_name:student_name,student_roll_no:student_roll_no
     ,student_class:student_class},
        dataType: "JSON",
        success: function(data) {
         $("#message").html(data);
        $("p").addClass("alert alert-success");
        },
        error: function(err) {
        alert(err);
        }
    });

, , , false,

if($stmt->execute())
 {
$res="Data Inserted Successfully:";
 echo json_encode($res);
}
 else {
 $error="Not Inserted,Some Probelm occur.";
echo json_encode($error);
  }
+2

,

{message:'hello world'}

,

JSON.parse(data.responseText).message

json javascript .

0

All Articles