I
I
Igor2017-05-05 22:52:38
JavaScript
Igor, 2017-05-05 22:52:38

How to get JSON using XMLHttpRequest in JS from this site openweathermap.org?

Good evening. What is the correct way to get Json using XMLHttpRequest? I write like this and in the end nothing works. What is wrong and how correctly tell me?

var myData = new XMLHttpRequest();
myData.open("GET", "api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=KEY", true);
myData.send();
if(myData.status != 200) {
  alert(myData.status + " " + myData.statusText);
} else {
  alert(myData.responseText);  
};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dark Hole, 2017-05-05
@abyrkov


Option 2 common origin policy
: 1. Use JSONP
2. Make a request to the server

S
Stalker_RED, 2017-05-05
@Stalker_RED

1. The request happens asynchronously, your if in the fourth line is triggered much earlier than the request reaches the server. You need to write an onload handler, you can see an example of using XMLHttpRequest https://developer.mozilla.org/ru/docs/Web/API/XMLH...
2. The address is wrong, try instead

api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=KEY
write
//api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=KEY
and of course substitute your key forKEY

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question