Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax data shows [object Object]

Tags:

jquery

ajax

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

like image 424
tawheed Avatar asked Aug 05 '13 16:08

tawheed


People also ask

What is object object in jQuery?

Introduction to jQuery object. The jQuery object is a collection of DOM elements and behaves like a special array. Everything in jQuery is an object. When we create a new element or select an existing element the jQuery returns those elements in a collection.

How do you fix an object object in HTML?

To fix this, you can use the JSON. stringify() method to change the object into a string that can be popped up in the browser using the alert() method.

What type of object does Ajax return?

ajax() function returns the XMLHttpRequest object that it creates. Normally jQuery handles the creation of this object internally, but a custom function for manufacturing one can be specified using the xhr option.


1 Answers

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

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

like image 77
MightyPork Avatar answered Oct 13 '22 07:10

MightyPork