蝴蝶不菲
2019-03-07 17:15:58
各種百度說這個(gè)錯(cuò)誤是因?yàn)閖son格式不標(biāo)注引起的這是后端php 用json_encode函數(shù)生成的json看了jquery的官方文檔似乎沒錯(cuò)jQuery.parseJSON( json )Passing in a malformed JSON string will result in an exception being thrown. For example, the following are all malformed JSON strings:{test: 1} (test does not have double quotes around it). 譯:test兩邊必須有雙引號 {"test":1}{'test': 1} ('test' is using single quotes instead of double quotes). 譯:test兩邊的引號不能為單引號,一定要是雙引號Additionally if you pass in nothing, an empty string, null, or undefined, 'null' will be returned from parseJSON. Where the browser provides a native implementation of JSON.parse, jQuery uses it to parse the string. For details on the JSON format, see?http://json.org/.而且以前一直都是這么返回的都沒有出錯(cuò),請問那么正確的json標(biāo)準(zhǔn)格式應(yīng)該是怎樣的?
3 回答

qq_花開花謝_0
TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊
jQ里的ajax,如果明確寫了
dataType: "json"
或者用的是getJSON的話,返回的data(參數(shù))可以直接當(dāng)對象用,不用轉(zhuǎn)換php里的json_encode有個(gè)小坑,用的時(shí)候需要把第二參數(shù)指定為
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK
,這個(gè)在官方文檔的JSON常量里有例程說明(評論里),不加這個(gè)的話,php會做自動(dòng)轉(zhuǎn)換,拿到的json不是帶斜線就是unicode被自動(dòng)轉(zhuǎn)換。另外后端返回其它奇怪的json也可以看看常量這塊。
添加回答
舉報(bào)
0/150
提交
取消