Saurabh Kumar
Saurabh Kumar

Reputation: 16671

How to get the Object atributes from ajax response

I make a call to the server using ajax and I return the class instance .

My return ajax response looks like follows.

  Object { id=1362570046980 , creationDate=1362570046980, type="PUBLIC"}

hOw i can get the values of id , creationDate and type from this object.

Upvotes: 1

Views: 4732

Answers (3)

bipen
bipen

Reputation: 36541

use . operator get the objects value.. however the obect which you are gettin is weird.. not JSON.. so make sure it is returning json

say your object is data

alert(data.id) //gives you 1362570046980 
alert(data.creationDate) //gives you 1362570046980 
alert(data.type) //gives you PUBLIC

Upvotes: 2

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

You need to specify a callback function in your ajax function to which an object will be passed containing the response.

$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" },
complete: function(data){ //This is the callback function
  alert(data.id);
  alert(data.creationDate);
  alert(data.type);
}
});

As you can see you can then access the properties in the json using simple dot notation.

Upvotes: 1

user1386320
user1386320

Reputation:

Try like this:

var myobject = yourAjaxResponseObject;

alert(myobject.id);
alert(myobject.creationDate);
alert(myobject.type);

Map a variable to your object/response, and access it's properties, etc, in that way as described up above.

Upvotes: 2

Related Questions