Skip to content

Instantly share code, notes, and snippets.

@MartinsAlexandre
Created March 6, 2018 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinsAlexandre/3348c2f921556e37952051a559b9731c to your computer and use it in GitHub Desktop.
Save MartinsAlexandre/3348c2f921556e37952051a559b9731c to your computer and use it in GitHub Desktop.
js http request
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";
}
}
<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