zoukankan      html  css  js  c++  java
  • jQuery1.4.2的json提示parsererror问题

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

    $.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]}

  • 相关阅读:
    Windows性能调优: Perfomn.exe 和Perfmon /res
    WPF:逻辑树和视觉树
    AD FS 概述
    SQL Server : TRUSTWORTHY 数据库属性
    WCF:在开发期间应该注意的问题
    ASP.NET MVC 2中的数据验证
    SQL Server:如何在Service Broker发送消息验证失败后获取源消息
    GDI+:自定义控件时如何使用Region来输出特定区域
    LINQ to XML:如何替换XCData的内容
    javascript是否真的就不能实现跨站请求呢?
  • 原文地址:https://www.cnblogs.com/EWall/p/2736681.html
Copyright © 2011-2022 走看看