首页 文章

OpenWeatherMap.ORG API - $ .getJSON没有't work and I' m没有收到任何数据

提问于
浏览
0

我正在尝试使用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 “:”破碎的 Cloud “,”icon“:”04n“}],”base“:”station“,”main“:{”temp“:289.26,”pressure“:1013,”humidity“:93,”temp_min“ “:289.26,” temp_max “:289.26},” 风 “:{” 速度 “:1.61,” DEG “:116.5},” 雨 “:{” 3H “:0.03},” Cloud “:{” 所有“: 76}, “DT”:1474367584, “SYS”:{ “类型”:3, “ID”:10843, “消息”:0.1585, “国家”: “AU”, “日出”:1474315673, “日落”: 1474359164}, “ID”:2172797, “名”: “凯恩斯”, “鳕鱼”: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)});


});

CodePen:https://codepen.io/elivanrock/pen/zKoYEj?editors=1011

提前致谢!

1 回答

  • 1

    你可以从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>
    

相关问题