我正在尝试使用一个简单的天气插件并使其变得智能,以便它通过调用基于 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+'°'+weather.units.temp+'</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li>';
html += '<li>'+weather.tempAlt+'°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/