-1

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;
    }

}
  • 2
    I don't see where you are trying to do anything with the response (or even where you are receiving the response). The code you posted seems to be incomplete. Please read through http://stackoverflow.com/help/mcve and update your question with a better example. – Felix Kling Oct 07 '14 at 15:40
  • 1
    @FelixKling you're really grumpy today. *I* thought the question was pretty clear; OP receives a JSON string from the server but isn't converting it into an Object before trying to access the keys. Go take a break, get some coffee, and spend a few more seconds on these questions before casting judgment. – 000 Oct 07 '14 at 15:44
  • @Joe: Pardon me for trying to maintain a certain level of quality. Btw, I didn't say the question wasn't clear. I said the example is not very good since it doesn't reflect the problem that was described. – Felix Kling Oct 07 '14 at 15:54
  • @FelixKling thank you for your intensions. I changed the code so that you can see what i did. I don't know if it helps – Felipe Gutierrez Oct 08 '14 at 07:24
  • And where exactly does it say `undefined`? – Felix Kling Oct 08 '14 at 07:27
  • on the page wait i show u a screenshot – Felipe Gutierrez Oct 08 '14 at 07:28
  • nevermind the JSON.parse() worked – Felipe Gutierrez Oct 08 '14 at 07:29

1 Answers1

2

I'm guessing that when you use the version that comes back from the server, it's just text - you'll need to use

weather = JSON.parse(theResponseData);

to turn it into an object that you can subsequently access properties on.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93