$ .getJSON()不工作铬,为什么呢?什么呢、工作、getJSON

由网友(欠你的幸福)分享简介:这code正常工作在FF和Safari浏览器,而不是在Chrome浏览器,为什么?$。的getJSON(s.json',功能(数据){变种项= [];$每个(数据,功能(键,VAL){items.push('<李ID =+按键+'>+ VAL +'< /李>');});$('< UL /&...

这code正常工作在FF和Safari浏览器,而不是在Chrome浏览器,为什么?

  $。的getJSON(s.json',功能(数据){
    变种项= [];

    $每个(数据,功能(键,VAL){
        items.push('<李ID =+按键+'>+ VAL +'< /李>');
    });

    $('< UL />',{
        类:我的新列表,
        的HTML:items.join('')
    })appendTo(身体)。
});
 

解决方案

我有完全相同的问题,这个问题是我的JSON是无效的(它隐藏制表符的话)。铬似乎比其他浏览器更严格。

请用 .error 的功能,例如响应,

  $。的getJSON(s.json',功能(数据){
  // ...
})
.error(功能(数据){
  的console.log(错误!);
});
 
.getJSON 问题

您也可以尝试验证的东西输出如 http://jsonlint.com/

This code works fine in FF and safari,Not in Chrome why?

$.getJSON('s.json', function(data) {
    var items = [];

    $.each(data, function(key, val) {
        items.push('<li id="' + key + '">' + val + '</li>');
    });

    $('<ul/>', {
        'class' : 'my-new-list',
        html : items.join('')
    }).appendTo('body');
});

解决方案

I had exactly the same problem, and the issue was that my json was not valid (it had hidden tab characters in it). Chrome seems to be more strict than the other browsers.

Check the response with a .error function eg,

$.getJSON('s.json', function(data) {
  //...
})
.error(function(data) {
  console.log("Error!");
});

You could also try validating the output with something like http://jsonlint.com/

阅读全文

相关推荐

最新文章