météo

include API with JAX

by AKTILOR

HTML

<html>
<h1>Rechercher la météo</h1>
  <form method="post" action="">
    <input type=text
name="ville" id="ville">
<input type="submit" value="Rechercher" id="submit">
</form>
</html>

JavaScript

document.getElementById('submit').onclick = function() {
var ville = document.getElementById('ville').value;
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'https://api.openweathermap.org/data/2.5/weather?q=' + encodeURIComponent(ville) + '&APPID=17a85be61992baf045f70c3d0367f7e8');
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      alert(JSON.parse(xhr.responseText).weather[0].description);
    }
  };
  xhr.send();
}