为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
绝地无双 2019-03-04 19:14:11
各种百度说这个错误是因为json格式不标注引起的 这是后端php 用json_encode函数生成的json 看了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标准格式应该是怎样的?
查看完整描述

3 回答

?
皈依舞

TA贡献1851条经验 获得超3个赞

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

查看完整回答
反对 回复 2019-03-18
?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

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

TA贡献1878条经验 获得超4个赞

代码示例
https://img1.sycdn.imooc.com//5c8f51050001433108000521.jpg

抛出的错误弹窗

https://img1.sycdn.imooc.com//5c8f51060001721803910080.jpg

https://img1.sycdn.imooc.com//5c8f51070001902b03990090.jpg

https://img1.sycdn.imooc.com//5c8f51090001290f04040085.jpg

https://img1.sycdn.imooc.com//5c8f510a000132f204030081.jpg

查看完整回答
反对 回复 2019-03-18
  • 3 回答
  • 0 关注
  • 466 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信