Javascript Internet Explorer 上的 JSON 解析错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7051500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON Parse Error on Internet Explorer
提问by Joao Moleiro
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:
我正在使用 jscript 从 Flickr 检索 JSON 数据。在除 IE 之外的所有浏览器中均 100% 工作。
我正在使用 jquery 每个为 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
}
}
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..
在 IE 中,函数返回var data = JSON.parse(xdr.responseText); 中的语法错误;但错误是随机的,它在显示错误之前检索随机数量的照片..
I've checked all the variables involved and everything is returning OK.
我已经检查了所有涉及的变量,一切都恢复正常。
I'm using the json2.js
我正在使用 json2.js
UPDATE:
更新:
JSON possible results:
JSON 可能的结果:
{
"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"
}
or
或者
{"stat":"fail", "code":1, "message":"Photoset not found"}
UPDATE:
thanks to all the help i was able to find the error and make a function compatible with IE 7+, Firefox, Chrome, etc..
更新:
多亏了所有的帮助,我才能找到错误并使功能与 IE 7+、Firefox、Chrome 等兼容。
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.: the 'test' var is string that i wanted to pass to the callback function
PS:'test' var 是我想传递给回调函数的字符串
回答by Naveen
Parsing JSON on IE 8 and below have issues. It fails to identify JSON functions.
在 IE 8 及更低版本上解析 JSON 有问题。它无法识别 JSON 函数。
Download the file https://github.com/douglascrockford/JSON-js/blob/master/json2.jsInclude it in your application and it should fix the problem.
下载文件https://github.com/douglascrockford/JSON-js/blob/master/json2.js 将其包含在您的应用程序中,它应该可以解决问题。
回答by Pir Abdul
You can choose a different method when using different browsers:
使用不同浏览器时可以选择不同的方法:
choose eval in IE6, 7 choose native JSON in IE8 choose new Function in Firefox2, 3 choose eval in Safari4 eval has the same performance as new Function on the whole when you use the other browsers.
在IE6中选择eval,7在IE8中选择native JSON在Firefox2中选择new Function,3在Safari4中选择eval eval在使用其他浏览器时总体上与new Function相同。
回答by Luca Rasconi
Using the file https://github.com/douglascrockford/JSON-js/blob/master/json2.jsyou'll have this error: "parsing string: Line: 191 Error: Object doesn't support this property or method". the property or method is valueOf().
使用文件https://github.com/douglascrockford/JSON-js/blob/master/json2.js你会得到这个错误:“解析字符串:行:191 错误:对象不支持此属性或方法” . 属性或方法是 valueOf()。
I use the solution suggested in JSON2 Error / Conflict with another script
我使用JSON2 Error / Conflict with another script 中建议的解决方案
return ((typeof this.valueOf === 'function') ? this.valueOf(): this.toString());
instead of
return this.valueOf();
return ((typeof this.valueOf === 'function') ? this.valueOf(): this.toString());
而不是
返回 this.valueOf();
回答by Juergen Riemer
If you load an iframe IE10 would accept a URL length of 4098.
如果您加载 iframe IE10 将接受 4098 的 URL 长度。