I've found this free weather API that gives you a forecast when you send an HTTP GET request - the forecast is delivered in JSON. A request could look like this:
Examples of JSON format:
Seaching by city name: api.openweathermap.org/data/2.5/weather?q=London,uk
Seaching by geographic coordinats: api.openweathermap.org/data/2.5/weather?lat=35&lon=139 Seaching by city ID: api.openweathermap.org/data/2.5/weather?id=2172797
So the response you get back is obviously defined by the end of the url, where some variables are located.
I've seen that you can use the jQuery function getJSON() to create an object out of the text file you get with an HTTP GET request, and you can also use variables with it to change the url. For an example doing this:
var person = "john";
$.getJSON("https://graph.facebook.com/" + person, function(person){
$.each(person, function(key, value){
document.write(key+": "+value+"<br />");
});
});
Gives the output
id: 779695190
first_name: John
gender: male
last_name: Chan
locale: en_US
name: John Chan
username: John
However, when I try to use this with the weather API it doesn't work - there is no output. (Doing it without url-variables here for simplicity.) The below code is how I thought it would be done (but apparantly not).
$.getJSON("api.openweathermap.org/data/2.5/weather?q=London,uk", function(forecast){
var temperature = forecast.main.temp;
document.write(temperature);
});
See the JSON structure for the forecast here: http://bugs.openweathermap.org/projects/api/wiki/Weather_Data
So basically my questions are: How would I append the url-variables to the weather-api-urls (and making sure the request can be both city name or coordinates)? And how would I access the retrieved data once I got it from the server?