Background:
I am working on a mini project learning how to use AJAX via vanilla JavaScript with a JSON file. I am aware I can use Jquery but I am learning the long way and Jquery will be next.
Goal:
My goal is to be able to type in a city and once I click the button it will render the current weather for the city I typed in.
Problem:
I cannot figure out and am looking for guidance on how to essentially take the value and attach it to a key and render all the data I am seeking. My thought process is to bring in the input value and compare it to the name key and if they are a match render the results. I just can't wrap my head around how that could look in code so any guidance would be greatly appreciated.
What I have:
So far I am able to render what I want by hard-coding with the following file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ajax3 - JSON(external api practice) File</title>
</head>
<body>
<input id="city">
<button id="button">Get Weather</button>
<br><br>
<h1>Weather</h1>
<div id='weather'></div>
<script>
// Create an event listener
document.getElementById('button').addEventListener('click', loadWeather);
function loadWeather(){
// console.log(city);
let xhr = new XMLHttpRequest();
xhr.open('GET', 'weather.json', true);
xhr.onload = function(){
let city = document.getElementById('city').value;
// let keys = Object.keys(weather);
if(this.status == 200){
let weatherTest = JSON.parse(this.responseText);
let result = '';
// weather.forEach((item, index) =>{
result += '<ul>'+
'<li>City: '+JSON.stringify(weatherTest[1].name )+ '</li>' +
'<li>Weather: '+JSON.stringify(weatherTest[1].weather[0].description )+ '</li>' +
'<li>Latitude: '+JSON.stringify(weatherTest[1].coord.lat )+ '</li>' +
'<li>Longitude: '+JSON.stringify(weatherTest[1].coord.lon )+ '</li>' +
'</ul>';
document.getElementById('weather').innerHTML = result;
// })
}
}
xhr.send();
}
//HTTP statuses
// 200: 'Ok'
// 403: 'Forbidden'
// 404: 'Not Found'
</script>
</body>
</html>
Here is the JSON file data I am working with:
[{
"coord":{
"lon":-62.08,
"lat":-31.43
},
"sys":{
"message":0.3897,
"country":"AR",
"sunrise":1428575101,
"sunset":1428616476
},
"weather":[
{
"id":802,
"main":"Clouds",
"description":"scattered clouds",
"icon":"03d"
}
],
"base":"stations",
"main":{
"temp":303.364,
"temp_min":303.364,
"temp_max":303.364,
"pressure":1015.65,
"sea_level":1026.91,
"grnd_level":1015.65,
"humidity":42
},
"wind":{
"speed":2.85,
"deg":32.0037
},
"clouds":{
"all":44
},
"dt":1428614645,
"id":3837675,
"name":"San Francisco",
"cod":200
},
{
"coord":{
"lon":-87.65,
"lat":41.85
},
"sys":{
"message":0.014,
"country":"US",
"sunrise":1428578344,
"sunset":1428625502
},
"weather":[
{
"id":501,
"main":"Rain",
"description":"moderate rain",
"icon":"10d"
}
],
"base":"stations",
"main":{
"temp":286.264,
"temp_min":286.264,
"temp_max":286.264,
"pressure":991.9,
"sea_level":1013.82,
"grnd_level":991.9,
"humidity":76
},
"wind":{
"speed":8.25,
"deg":202.004
},
"clouds":{
"all":92
},
"rain":{
"3h":4.61
},
"dt":1428615189,
"id":4887398,
"name":"Chicago",
"cod":200
}
]