我在尝试调用并解析一些JSON时遇到了一些问题。我收到的错误是 SCRIPT1014:无效字符此问题在所有浏览器中普遍发生,而不仅仅是Internet Explorer。
Jquery的:
$.ajax({
type: "POST",
url: "enquiries.php?show=all",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var obj = $.parseJSON(msg);
console.log(obj);
}
});
输出JSON:
{"0":{"ID":"1","first_name":"test","last_name":"test","email_address":"test","daytime_tel":"34343434","mobile_tel":"343434","comments":"sdfsdfsdfsdfsdfsdf","location_url":"32423452322resdfsdfxdf","date_of_submit":"2013-05-30","time_of_submit":null,"marked_as_read":"0","sender":"test test"}}
我已使用JSONLint将其检查为有效的JSON:
{
"0": {
"ID": "1",
"first_name": "test",
"last_name": "test",
"email_address": "test",
"daytime_tel": "34343434",
"mobile_tel": "343434",
"comments": "sdfsdfsdfsdfsdfsdf",
"location_url": "32423452322resdfsdfxdf",
"date_of_submit": "2013-05-30",
"time_of_submit": null,
"marked_as_read": "0",
"sender": "test test"
}
}
我还尝试过包含一个单独的文件来打印JSON(而不是enquiries.php?view = all方法)同样的问题仍然存在。
答案 0 :(得分:1)
正如@Malti和@CBroe上面指出的那样,问题与dataType有关:' json' - 问题解决了。
答案 1 :(得分:0)
除非您使用json_encode在enquiries.php中输出,否则您不需要解析。我建议使用json_encode。