[英]Jquery $.getJSON is not working Chrome and Firefox but it works in IE
我试图在我的网站中使用yahoo Wheather api,因此我编写了一个简单的脚本,如下所示:
<button>show</button><br />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var url='http://weather.yahooapis.com/forecastjson?w=2344116&u=c';
var query;
$('button').click(function(){
$.getJSON(url, function(json) {
alert(json.units.temperature);
});
});
});
</script>
它可以在IE上运行(我会弹出警报弹出窗口),但在chrome和firefox中不起作用。 另外,我验证了JSON输出;
{
"units": {
"temperature": "C",
"speed": "km/h",
"distance": "km",
"pressure": "mb"
},
"location": {
"location_id": "TUXX0014",
"city": "Istanbul",
"state_abbreviation": "*",
"country_abbreviation": "TU",
"elevation": 92,
"latitude": 41.01,
"longitude": 28.95
},
"wind": {
"speed": 10,
"direction": "NE"
},
"atmosphere": {
"humidity": "34",
"visibility": 9.99,
"pressure": 1016,
"rising": "steady"
},
"url": "http://weather.yahoo.com/forecast/TUXX0014.html",
"logo": "http://l.yimg.com/a/i/us/nt/ma/ma_nws-we_1.gif",
"astronomy": {
"sunrise": "06:29",
"sunset": "18:03"
},
"condition": {
"text": "Fair",
"code": "34",
"image": "http://l.yimg.com/a/i/us/we/52/34.gif",
"temperature": 9
},
"forecast": [
{
"day": "Today",
"condition": "Mostly Sunny",
"high_temperature": 8,
"low_temperature": 2
},
{
"day": "Tomorrow",
"condition": "Mostly Cloudy",
"high_temperature": 8,
"low_temperature": 3
}
]
}
任何帮助将不胜感激。
最好的祝福。
奥斯曼
我试过了: http : //jsfiddle.net/arXt2/ ,我的错误日志中说:
XMLHttpRequest cannot load http://weather.yahooapis.com/forecastjson?w=2344116&u=c.
Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.
您需要的是cross side scripting
。 请参阅如何使用带有跨站点脚本的JQuery制作ajax?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.