12

Imagine I run 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 the alert(responseText) returns:

[object Object]

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

Frantisek
  • 7,485
  • 15
  • 59
  • 102

3 Answers3

23

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/

Fábio Batista
  • 25,002
  • 3
  • 56
  • 68
  • thanks, that works! so what is "r" in this case? a short for "reply" or something? (just curious) – Frantisek Mar 20 '11 at 05:22
  • thanks, at least one of these two works (the latter one). the former one doesn't. – Frantisek Mar 20 '11 at 05:29
  • Right after I posted, I noticed that the answer might be wrong... So I checked the docs and edited it. It was interesting to see that you said it did work :) I'll edit again to avoid confusion for the readers. – Fábio Batista Mar 20 '11 at 05:30
  • So, @RiMMER, which solution worked out for you? Adding `dataType: 'text'` or treating the parameter as a `XMLHttpRequest`? – Fábio Batista Mar 20 '11 at 05:32
  • adding dataType: 'text' returns the exactly same value (object object). but the r.responseText works perfectly, and it worked right away, even before you edited anything. – Frantisek Mar 20 '11 at 05:38
  • Alright, I'll edit the answer so others might benefit from it as well. BTW, `r` is just a shorthand for `response`, I like to give short names to variables with a short lifespan. – Fábio Batista Mar 20 '11 at 05:43
2

Use on your client side ajax like this

 $.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);
        }
    });

in server side after query excecute you may use it give success when you query success false when your query has fault

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

I think you are receiving this in your server respones

{message:'hello world'}

if thats the case then use

JSON.parse(data.responseText).message

to convert the json string into javascript object and access your message property.

Bruce Tong
  • 1,366
  • 15
  • 14