OpenWeatherMap.ORG API - $.getJSON 不起作用,我没有收到任何数据
OpenWeatherMap.ORG API - $.getJSON doesn't work and I'm not recieving any data
我正在尝试使用提供的 api.
连接到 openweather.org 的测试代码
如果我使用浏览器访问 url:
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":"broken clouds","icon":"04n"}],"base":"stations","main":{"temp":289.26,"pressure":1013,"humidity":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,"name":"Cairns","cod":200}
问题是当我使用 jquery 的 $.getJSON 时,我看不到任何数据。
这是为什么?如何解决?
JS:
$(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)});
});
代码笔:https://codepen.io/elivanrock/pen/zKoYEj?editors=1011
提前致谢!
您可以使用 JSONp 从 openweathermap.com 获取数据,只需以这种方式添加您的回调函数:
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>
我正在尝试使用提供的 api.
连接到 openweather.org 的测试代码如果我使用浏览器访问 url:
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":"broken clouds","icon":"04n"}],"base":"stations","main":{"temp":289.26,"pressure":1013,"humidity":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,"name":"Cairns","cod":200}
问题是当我使用 jquery 的 $.getJSON 时,我看不到任何数据。
这是为什么?如何解决?
JS:
$(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)});
});
代码笔:https://codepen.io/elivanrock/pen/zKoYEj?editors=1011
提前致谢!
您可以使用 JSONp 从 openweathermap.com 获取数据,只需以这种方式添加您的回调函数:
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>