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
JAVASCRIPT:
JavaScript
x
22
22
1
function handleGetData(event){
2
event.preventDefault();
3
4
const cityName = $("#city").val()
5
$.ajax({url: `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=imperial&appid=99df413c60279878184277e08a2c6863`})
6
.then(
7
(data) => {
8
console.log(data);
9
$("#name").text(data.name)
10
$("#temp").text(data.temp)
11
$("#feels").text(data.feels_like)
12
$("#weather").text(data.weather)
13
14
},
15
(error) => {
16
console.log("bad request: ", error)
17
}
18
)
19
console.log("It's working!")
20
}
21
$('form').on("submit", handleGetData)
22
Advertisement
Answer
You are using a promise incorrectly.
The correct syntax is .then(result => {}).catch(e => {})
Like this
JavaScript
1
14
14
1
const cityName = $("#city").val()
2
$.ajax({url: url})
3
.then((data, a, b, c) => {
4
console.log(data);
5
$("#name").text(data.name)
6
$("#temp").text(data.temp)
7
$("#feels").text(data.feels_like)
8
$("#weather").text(data.weather)
9
console.log("It's working!")
10
})
11
.catch(error => {
12
console.error(error) //not necessarily a "bad request"
13
})
14