Hi im doing a little exercise for my self with JSON and i'm trying to reatrieve data from an URL
http://api.openweathermap.org/data/2.5/weather?q=miami
the query has no Problem and the data comes back i can even write it into the html file with innerHTML. But if i want to acces keys of the JSON data it says its undefined. But if i give the Variable the JSON code with Formating it works and i can even acces the keys i dont get it.
/* Here Goes Your Ajax Code */
var server;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
server = new XMLHttpRequest();
} else {// code for IE6, IE5
server = new ActiveXObject("Microsoft.XMLHTTP");
}
var city = "Düsseldorf"
var userInput = document.getElementById("userInput");
userInput.addEventListener("change", myFunction);
var weatherUrl = "http://api.openweathermap.org/data/2.5/weather?q="+city;
server.open("POST", weatherUrl , false);
server.send();
var weather= JSON.parse(server.responseText);
document.getElementById("errorMe").innerHTML = weather;
city = weather.name;
document.getElementById("city").innerHTML = city;
function myFunction() {
city = document.getElementById("userInput").value;
weatherUrl = "http://api.openweathermap.org/data/2.5/weather?q="+city;
server.open("POST", weatherUrl , false);
server.send();
document.getElementById("myDiv").innerHTML = weather= JSON.parse(server.responseText);
weather= JSON.parse(server.responseText);
if(weather.cod == 404){
document.getElementById("myDiv").innerHTML = "<p>You typet a city that does not Exist!</p>";
}else{
document.getElementById("myDiv").innerHTML = weather;
}
}