首页 > 解决方案 > 我怎样才能获得当前是否?我在这里做了一些代码,但卡在 fetch()

问题描述

const getWeather = (location) => {
    let url = ''
    let output = ''

    if (isNumeric(location)) {
        url = `http://api.openweathermap.org/data/2.5/weather?zip=${location}&units=imperial${api_key}`
    } else {
        url = `http://api.openweathermap.org/data/2.5/weather?q=${location}&units=imperial${api_key}`
    }

    console.log(url)

    fetch(url)
        .then(response => response.json())
        .then(data => {
            console.log(data)
            // YOUR CODE GOES HERE (REMOVE THIS ONCE YOU ADD YOUR CODE)
            output += `<h2>Current Weather for ${data.city.name}</h2>

我不知道标题的输出是对还是错?我只想得到我当前的是否。任何帮助我都很感激!

标签: javascriptfetch-api

解决方案


正如他们上面所说,您需要获取输出并将其传递给 html。就像是

  ` document.querySelector("#myDiv").innertHTML = output
`

其中 myDiv 是 HTML 中 div 的 id 属性 <div id='myDiv' > </div>


推荐阅读