• jQuery1.4.2与json格式兼容问题 (转载)


    原来使用jQuery1.3.2编写的代码,更换到1.4.2后,使用jQuery.ajax()加载的json文件,不能正常加载。(使用jQuery.getJSON()也一样)

    原json文件内容为:

    {
        label: 'Europe (EU27)',
        data: [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]
    }

    解决方法一:

    改成标准的json格式,要求对字符串都使用""限定,修改后的内容为:

    {
        "label": "Europe (EU27)",
        "data": [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]
    }

    这样就可以正常加载了。

    解决方法二:

    在jQuery-1.4.2.js中找到"parseJSON: function",可发现有如下代码:

    Js代码 复制代码
    1.         // Logic borrowed from http://json.org/json2.js   
    2. if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")   
    3.     .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")   
    4.     .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {   
    5.   
    6.   
    7.     // Try to use the native JSON parser first   
    8.     return window.JSON && window.JSON.parse ?   
    9.         window.JSON.parse( data ) :   
    10.         (new Function("return " + data))();   
    11.   
    12. } else {   
    13.     jQuery.error( "Invalid JSON: " + data );   
    14. }  
    				// Logic borrowed from http://json.org/json2.js 		if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@") 			.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]") 			.replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {  	 			// Try to use the native JSON parser first 			return window.JSON && window.JSON.parse ? 				window.JSON.parse( data ) : 				(new Function("return " + data))();  		} else { 			jQuery.error( "Invalid JSON: " + data ); 		}


    在httpData: function中用到了parseJSON函数:

    Js代码 复制代码
    1. // Get the JavaScript object, if JSON is used.   
    2.             if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {   
    3.                 data = jQuery.parseJSON( data );  
    // Get the JavaScript object, if JSON is used. 			if ( type === "json" || !type && ct.indexOf("json") >= 0 ) { 				data = jQuery.parseJSON( data );
     

    在jQuery1.3.2中,没有parseJSON这个方法,而是直接使用下面的代码。

    Js代码 复制代码
    1. // Get the JavaScript object, if JSON is used.   
    2.             if ( type == "json" )   
    3.                 data = window["eval"]("(" + data + ")");  
    // Get the JavaScript object, if JSON is used.             if ( type == "json" )                 data = window["eval"]("(" + data + ")");

    替换成原来1.3.2的代码就可以了。

     转自:http://cleaneyes.javaeye.com/blog/663883

  • 相关阅读:
    ViewState EnableViewState 禁用与使用心得
    ashx获取处理数据的简单例子
    移动标签(marquee)属性详解
    SQL Server 2008 R2 企业版/开发版/标准版(中英文下载,带序列号)
    C#,Dictionary,asp.net 字典 用法及简单操作
    .net 后台提交表单,获取返回结果
    ASP.NET : 如何将服务端的多个文件打包下载
    puppet原理及配置
    linux开机启动详细流程
    SHELL日志分析 实例一
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1776196.html
Copyright © 2020-2023  润新知