【问题标题】:JSON Parse Error on Internet ExplorerInternet Explorer 上的 JSON 解析错误
【发布时间】:2011-08-13 15:47:57
【问题描述】:

我正在使用 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
  }
}

在 IE 中,函数在 var data = JSON.parse(xdr.responseText); 中返回语法错误,但错误是随机的,它会在显示错误之前检索随机数量的照片。 .

我检查了所有涉及的变量,一切正常。

我正在使用 json2.js

更新:

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"
}

{"stat":"fail", "code":1, "message":"Photoset not found"}

更新:
感谢所有的帮助,我能够找到错误并使函数与 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.:'test' var 是我想传递给回调函数的字符串

【问题讨论】:

  • 可以添加一些示例 json 文件吗?
  • 你在使用 jQuery 吗? ($.browser.msie) 如果是这样,为什么不将它也用于 Ajax 请求?
  • 我使用 jQuery 对 chrome 和 firefox 等浏览器的 Ajax 请求,我尝试将它用于 IE,但没有成功
  • @João:jQuery 很可能比您的自定义解决方案更有效。
  • 我已经更新了 jQuery Ajax 请求(目前不适用于 IE 浏览器)

标签: javascript json internet-explorer parsing


【解决方案1】:

在 IE 8 及更低版本上解析 JSON 存在问题。它无法识别 JSON 函数。

下载文件https://github.com/douglascrockford/JSON-js/blob/master/json2.js 将它包含在您的应用程序中,它应该可以解决问题。

【讨论】:

    【解决方案2】:

    您可以在使用不同的浏览器时选择不同的方法:

    在 IE6、7 中选择 eval 在 IE8 中选择原生 JSON 在 Firefox2、3 中选择新功能 在 Safari4 中选择 eval 使用其他浏览器时,eval 整体性能与 new Function 相同。

    【讨论】:

      【解决方案3】:

      使用文件https://github.com/douglascrockford/JSON-js/blob/master/json2.js 你会有这个错误: “解析字符串:行:191 错误:对象不支持此属性或方法”。 属性或方法是 valueOf()。

      我使用JSON2 Error / Conflict with another script中建议的解决方案

      return ((typeof this.valueOf === 'function') ? this.valueOf(): this.toString());
      而不是
      返回 this.valueOf();

      【讨论】:

        猜你喜欢
        • 2011-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 1970-01-01
        • 2012-05-30
        • 1970-01-01
        • 2020-05-29
        相关资源
        最近更新 更多