jQuery1.4.2的json提示parsererror问题

By , 2010/06/10 - 75,793 views

今天用到jquery来处理ajax,用到了json。但是很诧异,jquery的ajax回调时一直调用了error函数(一直提示parsererror异常),success函数一次没执行过 👿 。代码如下:

$.ajax({
type:'POST',
url:'/go.php?p=chat.chatCenter&a=send',
dataType:'json',
data:'style='+Tstyle+'&content='+content,
success:function(data){$.chatCenter.sendEnd(data);},
error:function(data,t){alert("系统异常["+t+"]");$.chatCenter.sendEnd();}
});

服务器返回结果为:{id:”1″,data:”none”}

搞了很久,我把库换回1.3.1可以成功~~lol,最后发现是jQuery1.4.2这个版本引起的,1.4.2中jq把JSON的解析由原来的eval改为极其严格的$.parseJSON()来处理了。我在官方的说明文档中找到的,如下:

dataType

“json”: Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

http://api.jquery.com/jQuery.ajax/
就是说 jQuery 1.4对服务端返回的JSON 数据要求比较严格,必须严格按照JSON的标准来了。

下面我把$.parseJSON的说明文档也摘要下来:

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/.

现在什么都讲究标准啦,我们老老实实按标准来吧。我下面列举几个符合标准的例子:

{“myvalue”:1}
{“myvalue”:”red”}
{“myvalue”:[“black”,250]}



本文如无特别说明,均属原创,转载请注明出处!
原文链接:http://iiu.me/archives/114

共有 11 条精彩评论

  1. fovlife说道:

    原来如此
    顶一个!
    我说怎么总往error那跳!

    万能的eval

  2. Onepiece说道:

    我也遇到这个问题,有幸看到这篇文章真是太好了。

  3. figol说道:

    我也碰到了这样的问题,PARSEERROR 头痛死了。在FF下面能返回正确的处理结果,但是在IE下面 却还是执行ERROR;
    代码如下:$.ajax({
    type: “POST”,
    url: SELF_AJAX,
    data: {“funcCall”: “addCategory”,”category_name”:”aaaaa”,”category_sort”:category_sort,”category_fid”:category_fid },
    dataType:”json”,
    beforeSend:function(){$(“#btn_save”).hide();$(“#loading”).show();},
    success: function(data) {
    if(data.result == true){
    showMessageBox(“添加成功”,”分类添加成功!”);

    }else{
    showMessageBox(“添加失败”,data.message);
    }
    },
    complete:function(){$(“#btn_save”).show();$(“#loading”).hide();},
    error:function(XMLHttpRequest, textStatus, errorThrown){
    showMessageBox(“错误提示”,”XMLHttpRequest:”+XMLHttpRequest+” ” + “textStatus:”+textStatus+” ” + “errorThrown:”+errorThrown+” “);

    }
    });
    错误代码是:XMLHttpRequest: textStatus:parsererror errorThrown:[object Error]
    求高人指点。

  4. 山二代说道:

    一定要顶一个,解决我的大问题了

  5. 山二代说道:

    楼主真是细心

  6. 乡下人说道:

    晕菜,找了一天,才搜索到lz的这篇文章,才把问题解决了,感谢lz,把楼主放到友情链接,下次再来看看这里的好东西

  7. 乡下人说道:

    继续顶,很多人也遇到了这个问题

  8. […] 转载自http://iiu.me/archives/114/comment-page-1#comment-2421 […]

  9. 露珠说道:

    遇到这个问题好久了,今天看到你这篇文章解决了,非常感谢!

请您评论

 

健康游戏忠告:抵制不良游戏 拒绝盗版游戏 注意自我保护 谨防受骗上当 适度游戏益脑 沉迷游戏伤身 合理安排时间 享受健康生活
W3C 标准
[Valid RSS]
Valid XHTML 1.0 Transitional
Valid CSS!
分享
E:时间已做了选择 什么人叫做朋友 偶尔碰头 心情却能一点就通
H:因为我们曾有过 理想类似的生活 太多感受 绝非三言两语能形容
S:可能有时我们顾虑太多 太多决定需要我们去选择 担心会犯错 难免会受挫 幸好一路上有你陪我
合:与你分享的快乐 胜过独自拥有 至今我仍深深感动
好友如同一扇窗 能让视野不同 与你分享的快乐 胜过独自拥有 至今我仍深深感动 好友如同一扇门 让世界(变)开阔

无觅相关文章插件,快速提升流量