我正在尝试使用Providen api连接到openweather.org的测试代码。
如果我使用浏览器访问网址:
http://api.openweathermap.org/data/2.5/weather?id=2172797&APPID=35000cdad97645316c048563e4183021
然后,我得到正确的Json:{“ coord”:{“ lon”:145.77,“ lat”:-16.92},“ weather”:[{“ id”:803,“ main”:“ Clouds”,“ description “:”碎云“,” icon“:” 04n“}],” base“:”站点“,” main“:{” temp“:289.26,” pressure“:1013,”湿度“:93,” temp_min “:289.26,” temp_max“:289.26},” wind“:{” speed“:1.61,” deg“:116.5},” rain“:{” 3h“:0.03},” clouds“:{” all“: 76},“ dt”:1474367584,“ sys”:{“ type”:3,“ id”:10843,“ message”:0.1585,“ country”:“ AU”,“ sunrise”:1474315673,“ sunset”: 1474359164},“ id”:2172797,“名称”:“ Cairns”,“鳕鱼”:200}
问题是当我使用jquery的$ .getJSON时,我看不到任何数据。
这是为什么 ?如何解决?
$(document).ready(function(){
var api = 'http://api.openweathermap.org/data/2.5/weather?id=2172797&APPID=35000cdad97645316c048563e4183021';
$.getJSON(api, {format:'json'},function(data){console.log(data.coord.lon)});
});
CodePen:https://codepen.io/elivanrock/pen/zKoYEj ? editors = 1011
提前致谢!
您可以从openweathermap.com使用JSONp获取数据,只需通过以下方式添加回调函数即可:
http://api.openweathermap.org/data/2.5/weather?id=2172797&APPID=35000cdad97645316c048563e4183021&callback=myfunc
下面的例子:
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather",
jsonp: "callback",
dataType: "jsonp",
data: {
id: "2172797",
APPID: "35000cdad97645316c048563e4183021"
},
success: function( response ) {
console.log( response ); // server response
$('.current').html('<img src="http://openweathermap.org/img/w/' + response.weather[0].icon + '.png" /> ' + response.weather[0].main);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="current"></div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句