I've been experimenting with a basic console application that has two main functions: (1.) Sending an asynchronous Ajax request. (2.) Logging the responseText to the console.
To accomplish this, I decided to make an ajax request to openweathermap.org and then parse the responseText into JSON before logging it to the console.
Below is the code I used to achieve this:
var data ;
var url = "http://api.openweathermap.org/data/2.5/weather?id=7535661&APPID=56104080d6cb412468dad1627cb27da6";
var myRequest;
function sendRequest(url){
myRequest = new XMLHttpRequest();
myRequest.onreadystatechange= function(){
if(myRequest.readyState == 4 && myRequest.status== 200){
data= JSON.parse(myRequest.responseText);
}
}
myRequest.open("GET", url, true );
myRequest.send();
}
sendRequest(url);
console.log(data);
Every time I run this code, it always returns "undefined".
I would greatly appreciate any assistance on this matter.