Javascript OpenWeather JSONP

Javascript OpenWeather JSONP,javascript,jquery,json,Javascript,Jquery,Json,我正在使用他们的API访问数据,但尚未成功使其工作。我认为我访问的JSON不正确,但没有破解它 我正在使用以下代码)查询他们的数据: function getWeather(lat, lon, callback) { var weather = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&cnt=1'; $.ajax({ dataTy

我正在使用他们的API访问数据,但尚未成功使其工作。我认为我访问的JSON不正确,但没有破解它

我正在使用以下代码)查询他们的数据:

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
    });
};
我运行的查询返回以下结果:

{
 "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
}
然后,我尝试将他们的回答格式化为:

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;
    });

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

改用
data.main.temp

同样的情况也适用于
系统
属性

最后,您缺少结尾处
innerHTML
中的最后一个L


上演示所有修复程序时,您也无法正确访问某些属性。例如,您应该访问
data.sys.country
而不是
data.sys[0].country
。您可以在整个代码中执行此操作。@Andy:Mmh,然后错误消息
Uncaught SyntaxError:Unexpected token:
没有意义或不相关。编辑:嗯,我第一次在Chrome中加载小提琴时,它没有发出JSOP请求,但当我再次运行它时,它会发出JSOP请求。无论如何,我会删除我的第一条评论。停止在这里修改东西,修改你访问
数据的方式:)哈哈哈,谢谢Jared,我正在修改JSON的布局,以便我能理解你们对我说的话。没必要,加布教我怎么做。谢谢大家!加布,谢谢你!为我写另一本书。谢谢你的帮助!
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