简体   繁体   English

如何在ajax成功调用中读取已解析的json数据

[英]how to read parsed json data in ajax success call

On Ajax success call, I'm getting already parsed data in JSON format from a Controller. 在Ajax成功调用中,我已经从Controller获取到已解析为JSON格式的数据。 I want to read that data, so while I'm doing below one, I am getting undefined as an error. 我想读取这些数据,所以当我做不到一个数据时,我得到的undefined是错误。 How can I solve this? 我该如何解决?

success : function(response) { 
    alert(response.operatorId); 
},

Here is an example of working code 这是工作代码的示例

 success: function(json) {
   console.log(JSON.stringify(json.topics));
   $.each(json.topics, function(idx, topic){
     $("#nav").html('<a href="' + topic.link_src + '">' + topic.link_text + "</a>");
   });
 }

It appears that the response coming into success function is not a JSON object. 看来进入成功函数的响应不是JSON对象。 COuld you check if you have following set in your ajax call. 您能否检查ajax调用中是否有以下设置。

dataType: 'json',
contentType : 'application/json'

Alternatively, you may use the following to parse the json string to json object and then use dot notation to access the properties 或者,您可以使用以下内容将json字符串解析为json对象,然后使用点表示法访问属性

success : function(response) { 
    var jsonData = $.parseJSON(response)
    alert(jsonData.operatorId); 
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM