我在嘗試調用並解析某些JSON時遇到一些問題。我收到的錯誤是SCRIPT1014:無效字符這個問題在所有瀏覽器中普遍存在,不僅僅是Internet Explorer。JSON解析錯誤SCRIPT 1014 INVALID CHARACTER
的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方法),同樣的問題依然存在。
你已經有'dataType:「json」',爲什麼要解析? – Mati
我認爲它實際上可能是@MichaelRamirez,因爲如果它已經是一個對象,將它提供給JSON.parse需要調用它的toString方法,並且這會導致類似'[object Object]'(可能因瀏覽器而異) 。 – CBroe
我指的是@CBroe上面可能enquiries.php不是UTF 8的答案 - 我沒有意識到我甚至有dataType。 –