2013-05-30 69 views
1

我在嘗試調用並解析某些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方法),同樣的問題依然存在。

+4

你已經有'dataType:「json」',爲什麼要解析? – Mati

+2

我認爲它實際上可能是@MichaelRamirez,因爲如果它已經是一個對象,將它提供給JSON.parse需要調用它的toString方法,並且這會導致類似'[object Object]'(可能因瀏覽器而異) 。 – CBroe

+0

我指的是@CBroe上面可能enquiries.php不是UTF 8的答案 - 我沒有意識到我甚至有dataType。 –

回答

0

你不應該需要解析,除非你在使用enquiries.php到json_encode輸出。我會建議使用json_encode。

+0

echo json_encode($ enquiries-> showAll(),JSON_FORCE_OBJECT); –

1

由於@Malti和@CBroe指出上述問題是與數據類型來做到:「JSON」 - 問題解決。

+0

刪除這不是最好的解決方案。只要讓jQuery爲你解析響應。 –