JavaScript JSON 对象未解析

JavaScript JSON object not parsing

这是我的 JS 代码,来自 AJAX 调用的成功部分:

success: function(msg){
    var data = JSON.parse(JSON.stringify(msg));
    $("#searchResults").html(data + " Value for 'a': " + data.color + "\nValue for 'b': " + data.message);
}

页面上打印的内容如下:

{"color":"Yellow","message":"Pending"} Value for 'a': undefined Value for 'b': undefined

为什么它们未定义?

dataconsole.log 似乎仍然是一个字符串,就像@Musa 说的,你需要删除 JSON.stringify

如果您的 ajax 响应是 JSON,您应该在请求中明确说明,以便 jQuery.ajax 自动为您解析。 dataType: 'json'

$.ajax({
...
    dataType: 'json',
    success: function(data){
        $("#searchResults").html(" Value for 'a': " + data.color + "\nValue for 'b': " + data.message);
    }
...