首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenWeather JSONP

OpenWeather JSONP
EN

Stack Overflow用户
提问于 2014-01-26 01:24:23
回答 1查看 1.4K关注 0票数 0

我正在使用他们的API访问OpenWeather数据,但没有成功地让它正常工作。我认为我访问JSON是错误的,但没有破解它。

我使用以下代码JSFiddle在这里)查询它们的数据:

代码语言:javascript
复制
function getWeather(lat, lon, callback) {
    var weather = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&cnt=1';
    $.ajax({
        dataType: "jsonp",
        url: weather,
        success: callback
    });
};

我运行的查询返回以下内容:

代码语言:javascript
复制
{
 "coord":
   {
    "lon":-6.27,"lat":13.34
   },
 "sys": 
   {
    "message":0.0088,
    "country":"ML",
    "sunrise":1390719134,
    "sunset":1390760592
   },
 "weather":
 [
   {
    "id":800,"main":"Clear",
    "description":"Sky is Clear",
    "icon":"01n"
   }
 ],
 "base":"cmc stations",
 "main": 
   {"temp":293.154,
    "temp_min":293.154,
    "temp_max":293.154,
    "pressure":989.21,
    "sea_level":1024.43,
    "grnd_level":989.21,
    "humidity":64
   },
 "wind":
   {
    "speed":4.1,
    "deg":52.0018
   },
 "clouds":
   {
    "all":0
   },
 "dt":1390695239,
 "id":2451478,
 "name":"Segou",
 "cod":200
}

因此,我尝试把他们的回应写成这样:

代码语言:javascript
复制
var lat = 13.3428;
var lon = -6.26612;

    getWeather(lat, lon, function (data) {
        var tempHTML = "";
        tempHTML = tempHTML + '<h3>WEATHER INFO:</h3>';
        tempHTML = tempHTML + 'Weather data received <br/>';

        tempHTML = tempHTML + '<h3>WEATHER:</h3>';
        tempHTML = tempHTML + 'Weather Id: ' + data.weather[0].id + '<br/>';
        tempHTML = tempHTML + 'Weather Main: ' + data.weather[0].main + '<br/>';
        tempHTML = tempHTML + 'Weather Description: ' + data.weather[0].description + '<br/>';
        tempHTML = tempHTML + 'Weather Icon: ' + data.weather[0].icon + '<br/>';

        tempHTML = tempHTML + '<h3>MAIN:</h3>';
        tempHTML = tempHTML + 'Temperature: ' + data.main[0].temp + 'degrees<br/>';
        tempHTML = tempHTML + 'Temperature Min: ' + data.main[0].temp_min + 'degrees<br/>';
        tempHTML = tempHTML + 'Temperature Max: ' + data.main[0].temp_max + 'degrees<br/>';
        tempHTML = tempHTML + 'Pressure: ' + data.main[0].pressure + 'Kpa<br/>';
        tempHTML = tempHTML + 'Humidity: ' + data.main[0].humidity + '%<br/>';

        tempHTML = tempHTML + '<h3>WIND:</h3>';
        tempHTML = tempHTML + 'Wind Speed: ' + data.wind[0].speed + 'kmh<br/>';
        tempHTML = tempHTML + 'Wind Direction: ' + data.wind[0].deg + 'degrees <br/>';

        tempHTML = tempHTML + '<h3>CLOUDS:</h3>';
        tempHTML = tempHTML + 'Cloud Coverage: ' + data.clouds[0].all + '%<br/>';

        document.getElementById('weather_output').innerHTM = tempHTML;
    });

如果您查看我的JSFiddle,我会得到一个控制台错误:

代码语言:javascript
复制
Uncaught SyntaxError: Unexpected token : api.openweathermap.org/data/2.5/weather?lat=13.3428&lon=-6.26612&cnt=1&callback=jQuery191009959305939264596_1390699334749&_=1390699334750:1
Uncaught TypeError: Cannot read property 'temp' of undefined (index):36
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-26 01:30:18

如果您查看返回的json,main键指向一个对象,而不是一个数组,因此使用[0]会导致错误。

使用data.main.temp代替.。

windsyscloud属性也是如此。

最后,你错过了innerHTML的最后一个L。

演示http://jsfiddle.net/KLtLD/17/上的所有修复程序

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21358482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档