javascript - 我怎样才能获得当前是否?我在这里做了一些代码,但卡在 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>
我不知道标题的输出是对还是错?我只想得到我当前的是否。任何帮助我都很感激!
解决方案
正如他们上面所说,您需要获取输出并将其传递给 html。就像是
` document.querySelector("#myDiv").innertHTML = output
`
其中 myDiv 是 HTML 中 div 的 id 属性
<div id='myDiv' > </div>
推荐阅读
- python - 如何使用 soup.find_all() 提供以 sometext 开头的 attrs
- javascript - 通过公共值js映射两个对象数组
- r - 如何获得每列的中位数和标准差?
- flutter - 我如何处理未来不返回?
- reactjs - Redux 商店中的多对多关系
- python - 如何在 MATLAB、Python 和 C 中有效地计算大的复数值?
- google-cloud-functions - pdfmake节点js库中的Google Cloud函数本地目录图像读取错误
- django - 使用 Grappelli 扩展时更改表单中的 Django Admin 模型千位分隔符
- python - 列中的值所需的最近日期
- image - Vue.js 无法读取从 STRAPI 带来的 json 对象的属性