27

I have a very basic ajax call to alert the data that was reported from the server

$.ajax({
       type: "POST",
       url: "/someform/act", //edit utl to url
       data: { changed: JSON.stringify(plainData) }, //edit to include
       success: function(data) {
          alert(data);             //data not $data
       },
       error: function() {
          //error condition code
       }
});

According to the docs on the jquery website regarding data field on the success callback, it says that data returned is the data from the server. However for some strange reason when I alert $data, I get [object Object]

I was expecting to see something like this, since that is what the server would send back

<status>0</status>

EDIT:

data is also passed along as the POST

5
  • What do you get if you use alert(data);? Commented Aug 5, 2013 at 16:24
  • The server sent back XML, jQuery then parsed said xml. What you have is an xml document. Try console.log(data)
    – Kevin B
    Commented Aug 5, 2013 at 16:24
  • I get success when I alert data
    – tawheed
    Commented Aug 5, 2013 at 16:27
  • Then your server isn't returning what you think it is, or, the problem is with something that isn't included in your code above.
    – Kevin B
    Commented Aug 5, 2013 at 16:33
  • I'm not sure whether that was cause however I have modified the code above to reflect my code
    – tawheed
    Commented Aug 5, 2013 at 16:40

4 Answers 4

50

You need to use JSON.stringify(data) in the alert to get anything readable.

Also, $data is a completely different variable name than data.

3
  • Not really, it depends on what data is. Maybe it is already a string. Commented Aug 5, 2013 at 16:23
  • I get success back as a response. I was hoping to get the xml back from the server
    – tawheed
    Commented Aug 5, 2013 at 16:23
  • I get {"0":{},"length":1} Commented Apr 12, 2022 at 3:51
29

alert() prints the string representation of the arguments - hence if you pass an object, you'll get [object Object].

To inspect data, use console.log(data) better.

8

If you server send a JSON, you need to put dataType: 'json' to your ajax call. Be aware there's some mistake in your ajax call.

        $.ajax({
               type: "POST",
               url: "/someform/act", // NOT 'UTL',
               data: {
                  key: value,
                  key2: value2
               },
               // or data: plaindata, // If 'plaindata' is an object.
               dataType: 'json',
               success: function(data) {
                  console.log(data); // As moonwave99 said
               },
               error: function() {
                  //error condition code
               }
        });

EDIT

When sending data, you should send an object. jQuery will handle the array to sned it to the server. So if plain data is an object, it should be like this

               data: plainData,
0
1

If you're sending data via $.ajax({...}), the Network tab of your browser inspector might be showing [object Object] in the Payload (Chrome) / Request (Firefox) sub-tab, like in the following image (Firefox):

enter image description here

The reason for this might be in the way you're forming your AJAX call. Specifically:

$.ajax({
  url: '/ajax/example-endpoint',
  data: {'fooKey':fooData,'barKey':barData},
  type: 'post',
  cache: false,
  contentType: false, // this one will turn your data into something like fooKey=fooData&barKey=barData
  processData: false, // and this one will make it [object Object]:""
  beforeSend: function() {
    // whatever it is you need to do
  },
  success: function(data) {
    // do stuff
  },
  error: function(desc, err) {
    // do stuff
  }
});

When combined, contentType: false and processData: false turn your data into [object Object], because you're actually telling your AJAX call to ignore the content type of whatever is being sent, and not to process it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.