jQuery 1.4.2 getJSON() 不能正常工作的原因

 

以前寫了一個jQuery版的 N 級聯動插件,在jQuery 1.3.2中運行正常。

最近jQuery update 到 1.4.2,發現不能取數據了。經過 google 和其官方文檔,找到原因:

jQuery.getJSON(url, data, callback)

http://api.jquery.com/jQuery.getJSON/

The callback is passed the returned data, which will be a JavaScript object or array as defined by the JSON structure and parsed using the$.parseJSON()method.

就是說 jQuery 1.4 是用parseJSON()來解析返回的json數據的,而parseJSON()對數據要求比較嚴格

http://api.jquery.com/jQuery.parseJSON/

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': 1}('test' is using single quotes instead of double quotes).不能用單引號

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 ofJSON.parse, jQuery uses it to parse the string. For details on the JSON format, seehttp://json.org/.

所以,返回的JSON數據必須嚴格按上面的要求定義,而1.3好象用的是eval來返回一個對象,所以就沒有那麼嚴格了。修改了一個返回的JSON格式,原來的插件不需要修改一行代碼,可以正常運行了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章