我正在尝试使用openweathermap按地理位置显示当前天气,但我一直收到错误&#39; data.main未定义&#39;,因为我正在尝试从此json文件中获取数据:< / p>
{"coord":{"lon":16.44,"lat":43.51},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":291.15,"pressure":1017,"humidity":68,"temp_min":291.15,"temp_max":291.15},"visibility":10000,"wind":{"speed":2.01,"deg":146.502},"clouds":{"all":0},"dt":1459758600,"sys":{"type":1,"id":5459,"message":0.0285,"country":"HR","sunrise":1459744171,"sunset":1459790713},"id":3190261,"name":"Split","cod":200}
我不明白我应该如何从页面中获取所需的参数,例如温度,风,湿度......任何帮助表示赞赏
这是小提琴:
答案 0 :(得分:0)
问题是navigator.geolocation.getCurrentPosition
是一个异步函数。这意味着当您致电:
var api = 'http://api.openwe...?lat=' + lat + ...;
变量lat
和long
仍然未定义,因此API会返回代码为404
的响应,其中没有您期望的data.main.temp
。
您可以在解析异步函数后进行API调用,请参阅更新的jsfiddle:http://jsfiddle.net/foc7weg2/(尽管由于应用程序ID错误可能会返回401错误)