---
title: "解决jQuery 1.4 json问题"
author: "ol_beta"
date: "2010-12-21T17:09:00Z"
tags: []
categories: ["Web前端"]
slug: "jquery-1-4-json"
draft: false
lastmod: "2011-10-06T08:21:14Z"
id: 1185721
url: "https://softbeta.iteye.com/blog/1185721"
---

最近用jQuery写了一个小例子，

```
$(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

执行的时候总是不能执行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](http://json.org/) for more information on proper JSON formatting.)

正确的json格式为：

1. 键名称：用**双引号** 括起
2. 字符串：用使用**双引号** 括起
3. 数字，布尔类型**不需要** 使用**双引号** 括起

所以正确的getColors.jsp

 

感谢回答问题的 [cj205](http://hi.csdn.net/cj205) [net\_lover](http://hi.csdn.net/net_lover) [showbo](http://hi.csdn.net/showbo)

参考资料：[http://www.code-design.cn/article/20100722/jquery-1-4-datatype-is-json-issue.aspx](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://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://topic.csdn.net/u/20101220/20/fce48d0f-067c-44c8-ba9e-5cd1d0968654.html)

[http://api.jquery.com/jQuery.ajax/](http://api.jquery.com/jQuery.ajax/)

[http://forum.jquery.com/topic/jquery-1-4-x-getjson-callback-function-doesn-t-work](http://forum.jquery.com/topic/jquery-1-4-x-getjson-callback-function-doesn-t-work)