【问题标题】:$.getJSON not working in Internet Explorer$.getJSON 在 Internet Explorer 中不起作用
【发布时间】:2012-05-06 02:39:44
【问题描述】:

我正在使用以下代码从 JSON 中获取数据。

 $(document).ready(function()
 {
   $.getJSON("http://www.example.com/data.php?id=113&out=json", function(data) {

        $.each(data.issue.page, function(i,item) {
            imagesJSON[i] = item["@attributes"];
        });

       alert(imagesJSON.length);
    });
 });

它适用于Mozilla、Chrome 和其他浏览器,但不适用于IE。 (不在任何版本中)。

【问题讨论】:

  • 如果我们能看到 JSON 结果...
  • 什么 jquery 版本,这个链接在你的脚本所在的域上吗?
  • 在回调中添加调试代码的结果?
  • 它什么也不会显示。甚至没有任何错误。
  • 可能是同源策略问题,但如果是这种情况,它应该在其他浏览器中不起作用。使用 $.ajax 将允许您设置 error: function(x) { } 块,然后捕获错误并查看 x.responseText 以查看是否返回错误。

标签: javascript jquery internet-explorer getjson


【解决方案1】:

$.getJSON 倾向于cache results in IE。请改用$.ajax

在您的情况下,相关调用应该是这样的:

// Not really sure if you've forgot to var 
var imagesJSON = [];

$.ajax({
  url: "www.example.com/data.php?id=113&out=json",
  cache: false,
  dataType: "json",
  success: function(data) {
    $.each(data.issue.page, function(i,item) {
        imagesJSON[i] = item["@attributes"];
    });

    alert(imagesJSON.length);
  },
  error: function (request, status, error) { alert(status + ", " + error); }
});

确保您拥有cache: false


更新:

这似乎是主机上的配置问题,OP 实际使用的请求 url。使用 IE 网络浏览器直接访问 url 会导致主机中止。您只能将问题报告给主机,例如向主机的网站管理员发送电子邮件。

【讨论】:

  • 我用这个。有什么问题吗$.ajax({ url: "http://www.xyz.com/data.php?id=113&out=json", cache: false, dataType: "json", success: function(data) { $.each(data.issue.page, function (i, item) { imagesJSON[i] = item["@attributes"]; alert(imagesJSON.length); }); error: function (request, status, error) { alert(status + ", " + error); }
  • @ketan:你确定你输入正确吗?关键错误的值(即您在那里的函数)应该在 $.ajax 函数的参数数组中。
  • 以上事情在 IE 中给了我错误,例如 Error, No transport
【解决方案2】:

我在一个页面上遇到了同样的错误,我添加了这些行:

<!--[if lte IE 9]>
<script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.0/jquery.xdomainrequest.min.js'></script>
<![endif]-->

它最终对我有用 ;) IE9 不再出错

这篇文章对我有帮助 jQuery Call to WebService returns "No Transport" error

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 2012-06-07
    • 2015-03-16
    • 2011-06-02
    • 2015-12-17
    • 2013-04-30
    • 2013-09-25
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多