javascript - IE 中的 HTTP 获取 JSON 问题,适用于 Chrome FF

标签 javascript jquery json plugins

我正在尝试使用一个简单的天气插件并使其变得智能,以便它通过调用基于 IP 的地理定位服务来确定用户所属的位置。适用于 Chrome、FF。但不是 IE。出了什么问题或者为什么 IE 出现以下问题? 我这里似乎遇到了跨域调用问题。 完整 fiddle :http://jsfiddle.net/XfhQK/1/

 jQuery.getJSON('http://freegeoip.net/json/', function(data) {
     jQuery.simpleWeather({
        zipcode: data["zipcode"],
        woeid: '',
        location: '',
        unit: 'f',
        success: function(weather) {
          html = '<h2>'+weather.temp+'&deg;'+weather.units.temp+'</h2>';
          html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
          html += '<li class="currently">'+weather.currently+'</li>';
          html += '<li>'+weather.tempAlt+'&deg;C</li></ul>';

          jQuery("#weather").html(html);
        },
        error: function(error) {
          jQuery("#weather").html('<p>'+error+'</p>');
        }
     });

});

最佳答案

您正在访问的 URL 使用 Access-Control-Allow-Origin(又名 CORS)。 IE 8 或 9 不支持此功能。但是 IE 10 支持。

无论如何,修复它的最简单方法是包含 this plugin为 jQuery 添加了 XDomainRequest 支持。

您还需要更改为使用 $.ajax

$.ajax({
    url: 'http://freegeoip.net/json/',
    dataType: 'json',
    type: 'GET',
    crossDomain: true
    success: function(data){
        // ...
    }

});

演示:http://jsfiddle.net/XfhQK/4/

<子> 插件主页:https://github.com/jaubourg/ajaxHooks
发现于:http://bugs.jquery.com/ticket/8283

关于javascript - IE 中的 HTTP 获取 JSON 问题,适用于 Chrome FF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17285409/

相关文章:

javascript - 如何在 Meteor 中创建选择控件

javascript - angular2路由器避免url编码

jquery - 列宽容器未扩展到所有列

java - 需要在 spring-boot 应用程序中将 Json 输入中的本地日期保存到 oracle DB

php - Json返回字符

javascript - 输入类型 ='text' 值未在循环中通过 id 获取

javascript - Firefox 插件 : Have content script retrieve data stored by addon script (using a context menu if possible)

javascript - 单击按钮创建一个新的进度条

jquery - 使用方括号 [] 选择 jquery 元素

javascript - jQuery每个json结果,少于6个结果