How can I use Json JQuery in api.openweathermap to get weather information
2 answers
Use ajax call to get JSON like this
$(document).ready(function(){
$.getJSON("http://api.openweathermap.org/data/2.5/forecast/daily?q=Montpellier&mode=json&units=metric&cnt=10",function(result){
alert("City: "+result.city.name);
alert("Weather: "+ result.list[0].weather[0].description);
});
});
Here's a fiddle: http://jsfiddle.net/cz7y852q/
If you don't want to use jQuery:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
//access json properties here
alert("Weather: "+ data.weather[0].description);
}
else if (xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.open("GET", "http://api.openweathermap.org/data/2.5/weather?id=524901&APPID=7dba932c8f7027077d07d50dc20b4bf1", true);
xmlhttp.send();
Use your own API key if it doesn't work.
+4
source to share
Just do ajax GET request:
var url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=Montpellier&mode=json&units=metric&cnt=10"
$.getJSON(url).then(function(data) {
console.log(data);
});
api.openweathermap.org implements CORS, which means you won't have cross domain issues and can simply request the API using AJAX.
0
source to share