jquery ajax无法获取回调json ,并且报parsererror错误

各种百度说这个错误是因为json格式不标注引起的

这是后端php 用json_encode函数生成的json

https://img3.mukewang.com/5c8f510300017a9204030087.jpg

看了jquery的官方文档似乎没错
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/.

而且以前一直都是这么返回的都没有出错,请问那么正确的json标准格式应该是怎样的?

绝地无双
浏览 485回答 3
3回答

皈依舞

如果看起来没错,那就把整个字符串都按字符打印编码,看看有没有不可见字符比如 BOM。

沧海一幻觉

jQ里的ajax,如果明确写了dataType: "json"或者用的是getJSON的话,返回的data(参数)可以直接当对象用,不用转换 php里的json_encode有个小坑,用的时候需要把第二参数指定为JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK,这个在官方文档的JSON常量里有例程说明(评论里),不加这个的话,php会做自动转换,拿到的json不是带斜线就是unicode被自动转换。另外后端返回其它奇怪的json也可以看看常量这块。

UYOU

代码示例 抛出的错误弹窗
打开App,查看更多内容
随时随地看视频慕课网APP