$ .getJSON()不工作铬,为什么呢? [英] $.getJSON() is not working with chrome,Why?

查看:145
本文介绍了$ .getJSON()不工作铬,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这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(错误!);
});
 

您也可以尝试验证的东西输出如 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/

这篇关于$ .getJSON()不工作铬,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆