Created
March 6, 2018 16:59
-
-
Save MartinsAlexandre/3348c2f921556e37952051a559b9731c to your computer and use it in GitHub Desktop.
js http request
This file contains 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
function requestApi (){ | |
var api = "http://api.openweathermap.org/data/2.5/weather?q="; | |
var apiKey = "&APPID=5dbe8aa12a4dde7bbc49a832b7e2d5f7"; | |
var units = "&units=metric"; | |
var eltCity = document.getElementById("city"); | |
var input = eltCity.value; | |
var url = api + input + apiKey + units; | |
console.log(url); | |
var req = new XMLHttpRequest(); | |
req.onreadystatechange = function(event){ | |
if(this.readyState === XMLHttpRequest.DONE){ | |
if(this.status === 200){ | |
quoteReceveid(this.responseText); | |
console.log("if 200"); | |
}else{ | |
console.log("else 200"); | |
} | |
}else{ | |
console.log("else done"); | |
} | |
}; | |
req.open("Get", url, true); | |
req.send(null); | |
function quoteReceveid(quote){ | |
quote = JSON.parse(quote); | |
quote = quote.main.temp; | |
console.log(quote); | |
console.log("coucou"); | |
document.getElementById("para").innerText = quote + " °C"; | |
} | |
} |
This file contains 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
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Meteo</title> | |
</head> | |
<body> | |
<form name="myForm" method="GET"> | |
<label for="city">City :</label> | |
<input type="text" id="city" name="city"> | |
<input type="button" id="submit" value="Submit" onclick="requestApi()"> | |
</form> | |
<p id="para"></p> | |
<script src="request.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment