【发布时间】:2021-10-01 07:25:29
【问题描述】:
我无法从 API 访问数据。这是我的带有假访问代码的代码。
$(function () {
var $data = ('#data');
$.ajax({
type: 'GET',
url: 'http://api.openweathermap.org/data/2.5/weather?lat=47.6062&lon=-122.3321&units=imperial&appid=a0bfe75fbff13d4040af7eb66d1d82b5',
success: function (data) {
$.each(data, function (i, item) {
$data.append('<li>Temp: ' + item.main['temp'] + '</li>');
});
}
});
});
我收到一条错误消息,提示“未捕获的类型错误:无法读取未定义的属性 'temp'” 这是 API。
{
"coord": {
"lon": -122.3321,
"lat": 47.6062
},
"weather": [
{
"id": 721,
"main": "Haze",
"description": "haze",
"icon": "50d"
}
],
"base": "stations",
"main": {
"temp": 61.36,
"feels_like": 61.2,
"temp_min": 55.49,
"temp_max": 66.31,
"pressure": 1022,
"humidity": 85
},
"visibility": 10000,
"wind": {
"speed": 1.01,
"deg": 319,
"gust": 5.01
},
"clouds": {
"all": 1
},
"dt": 1627137674,
"sys": {
"type": 2,
"id": 2004026,
"country": "US",
"sunrise": 1627130239,
"sunset": 1627185243
},
"timezone": -25200,
"id": 5809844,
"name": "Seattle",
"cod": 200
}
我希望能够访问 temp 和 description 并将它们附加或添加到我的页面。 它不需要 是一个列表,但我想在 css 中设置它的样式。 随意更改任何代码。
{"coord":{"lon":-122.33,"lat":47.61},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50d"}],"base":"stations","main":{"temp":64.09,"feels_like":63.91,"temp_min":57.43,"temp_max":69.62,"pressure":1022,"humidity":79},"visibility":10000,"wind":{"speed":1.99,"deg":325,"gust":3},"clouds":{"all":1},"dt":1627141350,"sys":{"type":2,"id":2004026,"country":"US","sunrise":1627130238,"sunset":1627185243},"timezone":-25200,"id":5809844,"name":"Seattle","cod":200}```
【问题讨论】:
-
你能显示正确的 json 结构吗?
-
是的,如果这就是您所说的,我已经发布了它。在底部。
标签: javascript html jquery ajax api