JSON字符串没有使用JQuery parseJSON()处理没有错误,没有任何错误

时间:2015-03-30 10:47:51

标签: php jquery ajax json response

我一直在检查堆栈溢出,看看是否有类似的问题,但似乎没有人拥有它。 我从mysql表读取字段,其中我有我的JSON字符串。字符串本身的格式为:

{"width":"10", "height":"20", "angle":"10", "solution":"vertical"}

这是脚本的jquery部分:

function readJSON(id) {
return $.ajax({
 url: "/core/reader.php?id=" + id,
 method: "GET",
 dataType: "html"
})
}

call = readJSON(4);
call.success(function(response){
    alert(response); //returns string in alert window
    var jsondata = $.parseJSON(response);
    alert(jsondata.width); //should return value of width (10), but nothing happens
});

有没有人知道我该怎么办?提前谢谢。

3 个答案:

答案 0 :(得分:1)

谢谢大家的回答。我在实际数据中遇到了问题。我有一些关键:值,其中值包含一些控制字符。所以JSON字符串并不真正与JSON标准兼容。它描述得很好here

我已经更改了我的php脚本,如果它们存在则替换这些字符串,现在它们都按预期运行。

答案 1 :(得分:0)

而不是

alert(jsondata.width);

使用此

alert(jsondata['width']);

答案 2 :(得分:0)

尝试更改您的数据类型,如下所示:

function readJSON(id) {
return $.ajax({
 url: "/core/reader.php?id=" + id,
 method: "GET",
 dataType: "json"
});
}

在使用html数据类型之前,使用json返回数据。