解决jQuery 1.4 json问题
2010-12-21 17:09
495 查看
最近用jQuery写了一个小例子,
getColors.jsp
执行的时候总是不能执行success,
error显示status=parsererror,error=Invalid JSON: {name:'oliver',age:12}
但是把jQuery换成1.3版本就可以正常工作!
开始是以为是jQuery的bug,经过求证才知道是jQuery1.4对json数据的校验更加严格了,官方说明如下:
"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.)
正确的json格式为:
键名称:用双引号
括起
字符串:用使用双引号
括起
数字,布尔类型不需要
使用双引号
括起
所以正确的getColors.jsp
感谢回答问题的
cj205
net_lover
showbo
参考资料:http://www.code-design.cn/article/20100722/jquery-1-4-datatype-is-json-issue.aspx
http://www.code-design.cn/article/20100721/jquery-1-4-2-ajax-plugin-datatype-json-error.aspx
http://topic.csdn.net/u/20101220/20/fce48d0f-067c-44c8-ba9e-5cd1d0968654.html
http://api.jquery.com/jQuery.ajax/
http://forum.jquery.com/topic/jquery-1-4-x-getjson-callback-function-doesn-t-work
$(function(){ $.ajax({ url:'server/getColors.jsp', type:"POST", success:function(data){ alert(data); alert('name:'+data.name); alert('age:'+data.age); }, dataType:'json', error:function(xhr,status,error){ alert('status='+status+',error='+error); } }); });
getColors.jsp
{name:'oliver',age:12}
执行的时候总是不能执行success,
error显示status=parsererror,error=Invalid JSON: {name:'oliver',age:12}
但是把jQuery换成1.3版本就可以正常工作!
开始是以为是jQuery的bug,经过求证才知道是jQuery1.4对json数据的校验更加严格了,官方说明如下:
"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.)
正确的json格式为:
键名称:用双引号
括起
字符串:用使用双引号
括起
数字,布尔类型不需要
使用双引号
括起
所以正确的getColors.jsp
{"name":"oliver","age":12}
感谢回答问题的
cj205
net_lover
showbo
参考资料:http://www.code-design.cn/article/20100722/jquery-1-4-datatype-is-json-issue.aspx
http://www.code-design.cn/article/20100721/jquery-1-4-2-ajax-plugin-datatype-json-error.aspx
http://topic.csdn.net/u/20101220/20/fce48d0f-067c-44c8-ba9e-5cd1d0968654.html
http://api.jquery.com/jQuery.ajax/
http://forum.jquery.com/topic/jquery-1-4-x-getjson-callback-function-doesn-t-work
相关文章推荐
- jquery.getJSON的缓存问题解决办法
- jQuery1.4与json格式兼容问题
- 如何解决ecshop中jquery冲突?使用jquery.json.js代替transport.org.js中部份代码,完美解决问题。
- jQuery.getJSON的缓存问题的解决办法
- 解决jQuery $.getJSON() 传递参数中文乱码问题
- Jquery $.getJSON 在IE下的缓存问题解决方法
- jQuery.getJSON的缓存问题的解决办法
- 简单几招,解决jQuery.getJSON的缓存问题
- 使用getJSON解决jquery跨域问题
- jQuery.getJSON的缓存问题的解决办法
- jQuery UI Autocomplete 使用 ajax 方法传输Json数据出现乱码问题的解决
- jquery用jsonp方式跨域获取json数据原理(解决jquery跨服务器权限受限问题)
- JQuery JSON数据缓存问题解决
- 解决jQuery.getJSON的缓存问题
- 记解决jQuery $.getJSON() 传递参数中文乱码问题的方法
- Jquery中解决Json的问题
- jQuery.getJSON的缓存问题的解决办法
- jQuery getJSON 处理json数据的代码 解决ajax跨域返回json 数据问题
- 使用getJSON解决jquery跨域问题
- 解决jQuery $.getJSON() 传递参数中文乱码问题