【发布时间】:2012-03-07 09:55:00
【问题描述】:
我正在尝试在我的网站中使用 yahoowheather 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
}
]
}
我们将不胜感激。
最好的问候。
奥斯曼。
【问题讨论】:
-
在 jsFiddle 中尝试这个在 Chrome 和 Firefox 中给我一个
Access-Control-Allow-Origin失败。