[英]Cant figure out why I cant retrieve data from my API
When I type a city name into my search bar it should pull up information on the weather on that city but I get a 400 bad request error当我在搜索栏中输入城市名称时,它应该会显示该城市的天气信息,但出现 400 bad request 错误
JAVASCRIPT:爪哇脚本:
function handleGetData(event){
event.preventDefault();
const cityName = $("#city").val()
$.ajax({url: `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=imperial&appid=99df413c60279878184277e08a2c6863`})
.then(
(data) => {
console.log(data);
$("#name").text(data.name)
$("#temp").text(data.temp)
$("#feels").text(data.feels_like)
$("#weather").text(data.weather)
},
(error) => {
console.log("bad request: ", error)
}
)
console.log("It's working!")
}
$('form').on("submit", handleGetData)
You are using a promise incorrectly.您错误地使用了承诺。 The correct syntax is
.then(result => {}).catch(e => {})
正确的语法是
.then(result => {}).catch(e => {})
Like this像这样
const cityName = $("#city").val()
$.ajax({url: url})
.then((data, a, b, c) => {
console.log(data);
$("#name").text(data.name)
$("#temp").text(data.temp)
$("#feels").text(data.feels_like)
$("#weather").text(data.weather)
console.log("It's working!")
})
.catch(error => {
console.error(error) //not necessarily a "bad request"
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.