NicklasF
NicklasF

Reputation: 921

Access JSON Object in ajax success

Somehow I can't find out how to access my json object from ajax call. In the handler, I'm echoing next id:

echo json_encode(array(
   "nextId" => 2
));

Then I want to access it with data:

$.ajax({
   [...],
   dataType: 'json',
   success:function(data) {
       console.log(data)
       console.log(data['responseText'];
       console.log(data['responseText'].nextId);       
       console.log(data['responseText']['nextId']);
   }
});

Result: Object {readyState: 4, responseText: "{"nextId":2}", responseJSON: Object, status: 200, statusText: "OK"}
Result: {"nextId":2}
Result: Undefined    
Result: Undefined

I want to get the value of nextId

Upvotes: 1

Views: 1628

Answers (1)

nibnut
nibnut

Reputation: 3127

Looking at your data object, I see an object in there called responseJSON... Try this instead:

$.ajax({
   [...],
   dataType: 'json',
   success:function(data) {
       console.log(data)
       console.log(data['responseJSON'];
       console.log(data['responseJSON'].nextId);       
       console.log(data['responseJSON']['nextId']);
   }
});

Hope this helps!

Upvotes: 2

Related Questions