所以,这在我看来应该很简单......我有一个通过 Ajax post 返回的有效 JSON 字符串:
{"success":true,"message":"Thank you! We value your feedback."}
我只是想将我的“消息”值提醒到我返回的结果中:
success: function (result) {
alert(result);
var obj = $.parseJSON(result);
alert(obj.message);
},
error: function (req, status, error) {
alert("Sorry! We could not receive your feedback at this time.");
}
我的“obj”属性以某种方式未被识别......我已经验证了 JSON 字符串以确保它是有效的,那么我在这里遗漏了什么?
最佳答案
您不需要解析 JSON。将 dataType
属性设置为 json
,jQuery 将为您解析它。然后,result
本质上是您的 JSON,您可以执行 alert(data.message);
。
jQuery.ajax({
...
dataType: "json",
success: function(data) {
alert(data.message);
},
...
});
关于javascript - 使用 jQuery.parseJSON() 解析 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3569416/