javascript - 无法将用户输入添加到 api
问题描述
这是代码,cosole.log 显示了城市的正确值。城市的值是从另一个 js 文件传递的。
import React from 'react';
import { useState,useEffect } from 'react';
import ReactDOM from 'react-dom';
const Content=(city)=>{
const [cities, setItems] = useState([]);
console.log(city);
useEffect((city) => {
fetch(`http://api.openweathermap.org/data/2.5/forecast?q=${city},LK&mode=json ,appid=5c4420d5c8a61c16e5ee37e4ca265763`)
.then(res => res.json())
.then(
(result) => {
setItems(cities => cities.concat(result));
},
)
},[]);
console.log(cities);
return (
<h2>hello</h2>
);
}
export default Content;
我认为问题出在 ${city} 上,但我尝试了我所知道的一切来解决它。
解决方案
我认为问题可能出在 URL 中,其中查询参数有错字(and 之间有一个逗号mode
,appid
它应该是 &)。
它应该是
fetch(`http://api.openweathermap.org/data/2.5/forecast?q=${city},LK&mode=json&appid=5c4420d5c8a61c16e5ee37e4ca265763`)
顺便说一句,mode=json
没有必要,因为 JSON 是默认的响应模式。
推荐阅读
- mocking - 被测类的假方法
- c# - 获得程序中最后 3 个测试用例的时间限制:C#
- android - Android:抑制协同程序的多次调用
- amazon-web-services - 带有特殊字符的 AWS SSM 参数问题
- python - pyspark-如何计算百分比
- javascript - 当我在 React 中更改回选项卡时,无法读取未定义的属性“0”
- javascript - 使用模式中的属性来计算 Mongoose 中特定模式的属性值
- python - 如何在python中通过Xpath获取div之间的文本
- java - FitNesse 不调用重载方法
- odoo-14 - Odoo14 | 找不到唯一 ID base.user_admin 的记录