【问题标题】:$.getJSON - ajax is sent but callback function is ignored - Internet Explorer$.getJSON - 发送 ajax 但忽略回调函数 - Internet Explorer
【发布时间】:2011-08-24 09:04:22
【问题描述】:

这是我的功能

function ajax(addr,loading, loadTo, json){
        addr = addr.replace(' ', '');
        if (loading){
                $("#"+loading).fadeIn();
        }

        if (json){
                $.getJSON(addr, function(data){
                        alert('whoooo working'); // <--- it never goes here
                        if (loading){
                                $("#"+loading).fadeOut();
                        }
                        procJSON(data);
                });
                return true;
        }
}

我用它来称呼它

var postid = $(this).attr('data-postid');
ajax(url+'tools/delete/'+postid, 'loading'+postid, false, true);

ajax 被发送,图像(加载图像)被显示,但从未调用回调函数。

这不只是 IE 大列表中的新保留值吗?是的,我知道,IE 不是一个有效的浏览器,但我不能责怪我的客户

【问题讨论】:

  • 响应有什么 Content-Type?应该是application/json,FWIW。 (此外,使用内置调试器很容易逐步完成 - 这是否提供任何线索?)
  • 我不知道,但硬核它是个好主意
  • @Tomalak:请回答我的问题。你是 100% 正确的。该死,我不知道 IE 这么特别
  • @Tomalak:关于你的问题,我为什么不使用调试器:我从来没有使用过 IE,所以我不知道 IE 有开发人员工具
  • IE 8 以上的 F12 有调试工具。不像 Firebug 或 Chrome 那样复杂,但它们可以胜任。你也可以让它模拟旧版本 IE 的行为(JS 和渲染引擎)。继续接受@Guffa 的回答。如果你愿意,可以给我投票over here

标签: javascript jquery ajax json internet-explorer-8


【解决方案1】:

由于它在特定浏览器中失败,很可能是响应中意外的标头的组合,以及浏览器如何根据该标头处理数据。

例如,如果响应的内容类型为 text/html 而不是 application/json,则浏览器可能会尝试将响应内容转换为 HTML 文档(通过在其周围添加 pre 标签),这将导致JSON 解析失败。

如果您使用$.ajax 方法,您还可以捕获任何错误消息,这将为您提供线索:

$.ajax({
  url: addr,
  dataType: 'json',
  success: function(data){
    alert('whoooo working'); // <--- it never goes here
    if (loading){
      $("#"+loading).fadeOut();
    }
    procJSON(data);
  },
  error: function(o,c,m) { alert(m); }
});

【讨论】:

  • +1 或者,您可以在调试工具中观察网络活动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 2012-09-20
  • 2012-03-30
  • 2018-05-05
相关资源
最近更新 更多