在ajax成功中访问JSON对象

时间:2021-09-25 19:57:53

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

不知何故,我无法找到如何从ajax调用访问json对象。在处理程序中,我重复下一个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

我想知道nextId的值

1 个解决方案

#1


2  

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

看着你的数据对象,我看到一个对象叫responseJSON…试试这个:

$.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!

希望这可以帮助!

#1


2  

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

看着你的数据对象,我看到一个对象叫responseJSON…试试这个:

$.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!

希望这可以帮助!