Internet Explorer上的JSON解析错误 [英] JSON Parse Error on Internet Explorer
问题描述
我正在使用jscript从Flickr检索JSON数据.在除IE之外的所有浏览器中都可以100%运行. 我正在使用jquery的每个为IE调用此特定函数的函数:
I'm using a jscript to retrieve JSON data from Flickr. Works 100% in every browsers except IE.
I'm using the jquery each function that calls this specific function for IE:
//some code
if ($.browser.msie && window.XDomainRequest) {
var xdr;
var url = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=" + apiKey + "&photoset_id=" + set + "&extras=url_sq&format=json&nojsoncallback=1";
xdr = new XDomainRequest();
if (xdr) {
xdr.open("get", url);
xdr.send();
var data = JSON.parse(xdr.responseText);
//some jquery stuff
}
}
在IE中,该函数在 var data = JSON.parse(xdr.responseText); 中返回语法错误,但该错误是随机的,它会在显示错误之前检索随机数量的照片.
In IE the function return's a syntax error in the var data = JSON.parse(xdr.responseText); but the error is random, it retrieves a random number of photos before showing the error..
我检查了所有涉及的变量,一切都恢复正常.
I've checked all the variables involved and everything is returning OK.
我正在使用json2.js
I'm using the json2.js
更新:
JSON可能的结果:
JSON possible results:
{
"photoset": {
"id": "72157627083924637",
"primary": "5943107169",
"owner": "63570294@N03",
"ownername": "motorespt.com",
"photo": [
{
"id": "5943107169",
"secret": "e6099e3936",
"server": "6029",
"farm": 7,
"title": "Peugeot 206",
"isprimary": "0",
"url_sq": "http://farm7.static.flickr.com/6029/5943107169_e6099e3936_s.jpg",
"height_sq": 75,
"width_sq": 75
}
],
"page": 1,
"per_page": 500,
"perpage": 500,
"pages": 1,
"total": "1"
},
"stat": "ok"
}
或
{"stat":"fail", "code":1, "message":"Photoset not found"}
更新:
感谢所有的帮助,我得以找到错误并使其功能与IE 7 +,Firefox,Chrome等兼容.
UPDATE:
thanks to all the help i was able to find the error and make a function compatible with IE 7+, Firefox, Chrome, etc..
function flickr_test(){
var apiKey = 'YOUR_API_KEY';
$.ajax({
url: 'http://api.flickr.com/services/rest/',
data: {
method: 'flickr.test.echo',
api_key: apiKey,
format: 'json',
test: 'test string',
jsoncallback: 'jsonFlickrApi'
},
dataType: 'jsonp'
});
}
function jsonFlickrApi(response){
console.log(response.stat);
}
P.S.:"test"变量是我想传递给回调函数的字符串
P.S.: the 'test' var is string that i wanted to pass to the callback function
推荐答案
在IE 8及更低版本上解析JSON时遇到问题.它无法识别JSON函数.
Parsing JSON on IE 8 and below have issues. It fails to identify JSON functions.
下载文件 https://github.com/douglascrockford/JSON-js/blob/master/json2.js 将其包含在您的应用程序中,它应该可以解决问题.
Download the file https://github.com/douglascrockford/JSON-js/blob/master/json2.js Include it in your application and it should fix the problem.
这篇关于Internet Explorer上的JSON解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!