【问题标题】:How do I retrieve and transpose data to a webpage from an API?如何从 API 检索数据并将其转置到网页?
【发布时间】: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


【解决方案1】:

要访问temp,您不需要使用.each 循环,您可以直接访问它们,即:data.main.temp 和访问天气-> 描述您可以使用.each 循环,在每个循环内使用@ 987654325@.

演示代码

var $data = ('#data');
//dummy json 
var data = {
  "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
}
/* $.ajax({
   //your codes....
   */
console.log(data.main.temp) //use it like this...
$.each(data.weather, function(i, item) {
  console.log(item.description) //get description like this..
})
/*
}
});*/
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

  • 快速问题,然后我如何将它放到我的网页上?我尝试了附加,但它告诉我它不是一个函数。
  • 嗨,你错过了$这里('#data');把那个再试一次。
  • $(function () { var $data = ('#data'); $.ajax({ type: 'GET', url: 'http://api.openweathermap.org/data/2.5/weather?lat=47.6062&amp;lon=-122.3321&amp;units=imperial&amp;appid=a0bfe75fbff13d4040af7eb66d1d82b1', success: function (data) { $data.appened('&lt;li&gt;' + data.main.temp + '&lt;li&gt;'); $.each(data.weather, function (i, item) { console.log(item.description)})}}); }); 这就是我所拥有的,它仍然给我非函数错误。抱歉格式化。
  • 这里$data.appened..追加拼写错误应该是$data.append(..另外,在这里添加$`var $data = $('#data');`
猜你喜欢
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 2015-07-20
相关资源
最近更新 更多