当我在搜索栏中输入城市名称时,它应该会提取有关该城市天气的信息,但出现400错误的请求错误
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)
答案 0 :(得分:0)
您使用的诺言不正确。
正确的语法是.then(result => {}).catch(e => {})
赞
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"
})