服务器返回的数据类型没有严格遵从json数据格式所致;
开始我只返回了一个字符串,这样就不符合json数据格式,但如果返回只是一个字符时就不会把这个错,
总而言之:数据格式错误。
在网上查找了一些资料在此总结:
格式要求:
在1.3.1之前数据格式允许:{id:"1",info:"你好"} 或者是{id:’1‘,data:‘none’}
jQuery1.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/.
数据格式标准必须严格按照json数据。:
{"id":9527}
{"color":"red"}
{"colorvalue":["black",250]}
当然,需要传json数据的地方也要注意这个问题
参考文章:http://lufei-99999.blog.163.com/blog/static/748495420122260957269/