使用jQuery进行Forecast.io API使用

我在使用API​​创建完整的应用程序时遇到了一些麻烦,特别是Forecast.io weather api。 为简单起见,我将我的JS直接放在我的HTML页面中。 对于这个基本版本,我很乐意让这个展示一些东西。 假设我想要当前的温度(目前 – >温度)。 另外,我不确定“?回调?” 始终建议所有RESTful API使用。

   

Here's the weather:

function b(){ var apiKey = ''; var url = 'https://api.forecast.io/forecast/'; var lati = 0; var longi = 0; var data; $.getJSON(url + apiKey + "/" + lati + "," + longi + "?callback=?", function(data) { $('#weather').innerHTML('and the weather is: ' + data[4].temperature); }); }

你犯的主要错误不包括jQuery :-)下一个是在jQuery对象上你需要使用html()函数而不是JavaScript本机innerHTML属性。

如果使用console.log(数据),则可以看到返回对象的所有属性,因此可以像data.currently.temperature一样正确引用它。

    

Here's the weather: