Created
December 12, 2018 00:35
-
-
Save DominicTremblay/34e9743d4b3612e556edd5e71c9cf255 to your computer and use it in GitHub Desktop.
Ajax call to weather API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var cityName = 'Montreal'; | |
var apiKey = 'put your api key here'; | |
var url = | |
'https://api.openweathermap.org/data/2.5/weather?q=' + | |
cityName + | |
'&appid=' + | |
apiKey; | |
function createListItems(items, $targetElement) { | |
$.each(items, function(key, value) { | |
var $li = $('<li>'); | |
var $h4 = $('<h4>').text(key); | |
var $p = $('<p>').text(value); | |
$li.append($h4).append($p); | |
$targetElement.append($li); | |
}); | |
} | |
function createResult(data) { | |
var cityName = data.name; | |
var country = data.sys.country; | |
var mainWeather = data.weather[0].main; | |
var description = data.weather[0].description; | |
var temperatures = data.main; // this is an object | |
var windData = data.wind; // this is an object | |
console.log(cityName, country, mainWeather, description); | |
// <section class="result" style="display: block;"> | |
// <header> | |
// <h1>Weather App</h1> | |
// <h2>Toronto, CA</h2> | |
// extract the city and country | |
// create an h2 element and add it to the header | |
var $h2Header = $('<h2>').text(cityName + ', ' + country); | |
$('.result header').append($h2Header); | |
// <div class="current-weather"> | |
// <h2>Snow</h2> | |
// <p>light snow</p> | |
// </div> | |
var $h2Div = $('<h2>').text(mainWeather); | |
var $pDiv = $('<p>').text(description); | |
$('.current-weather') | |
.append($h2Div) | |
.append($pDiv); | |
// <ul class="result-temperatures"> | |
// <li> | |
// <h4>Temperature</h4> | |
// <p>-1.17</p> | |
// </li> | |
// { | |
// "temp": 264.8, | |
// "pressure": 1016, | |
// "humidity": 92, | |
// "temp_min": 264.15, | |
// "temp_max": 265.15 | |
// } | |
createListItems(temperatures, $('.result-temperatures')); | |
createListItems(windData, $('.result-wind')); | |
// <ul class="result-wind"> | |
// <li> | |
// <h4>Speed</h4> | |
// <p>6.2</p> | |
// </li> | |
} | |
function getWeatherInfo(url) { | |
$.ajax(url) | |
.done(function(data) { | |
createResult(data); | |
}) | |
.fail(function(error) { | |
console.log('Error: ', error); | |
}) | |
.always(function() { | |
console.log('Request completed.'); | |
}); | |
} | |
getWeatherInfo(url); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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" /> | |
<link rel="stylesheet" href="./styles.css" /> | |
<script | |
src="https://code.jquery.com/jquery-3.3.1.min.js" | |
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" | |
crossorigin="anonymous" | |
></script> | |
<script src="./app-demo.js"></script> | |
<link | |
href="https://fonts.googleapis.com/css?family=Dosis:400,600,700|Yanone+Kaffeesatz:400,700" | |
rel="stylesheet" | |
/> | |
<title>Document</title> | |
</head> | |
<body> | |
<section class="result" style="display: block;"> | |
<header><h1>Weather App</h1></header> | |
<div class="current-weather"></div> | |
<div class="temperatures"> | |
<h2>Temperatures</h2> | |
<ul class="result-temperatures"></ul> | |
</div> | |
<div class="wind"> | |
<h2>Wind</h2> | |
<ul class="result-wind"></ul> | |
</div> | |
</section> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment