Hi I am buliding my first web app using javascript and fetching data using API from www.openweathermap.org/ I have used the API key as mentioned in the documentation still it is giving an error of unauthorization. Can there be any other reason for this error while calling a function or so . Thank you in advance.
JavaScript
x
33
33
1
var APPID = "my_secret_key";
2
var temp;
3
var loc;
4
var icon;
5
var wind;
6
var humidity;
7
var direction;
8
function updateByZip(zip){
9
var url = "http://api.openweathermap.org/data/2.5/weather?" +
10
"zip = " + zip +
11
"&APPID =" + APPID ;
12
sendRequest(url);
13
}
14
15
function sendRequest(url){
16
var xmlhttp = new XMLHttpRequest();
17
xmlhttp.onreadystatechange = function(){
18
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
19
var data = JSON.parse(xmlhttp.responseText) ;
20
var weather = {};
21
weather.wind = data.wind.speed;
22
weather.direction = data.wind.deg;
23
weather.loc = data.name;
24
weather.temp = data.main.temp;
25
weather.icon = data.weather[0].id;
26
weather.humidity=data.main.humidity;
27
update(weather);
28
}
29
};
30
xmlhttp.open("GET", url, true);
31
xmlhttp.send();
32
}
33
Advertisement
Answer
It’s the spaces near the equal signs in your URL. It’s likely urlencoding the space and sending your parameter as APPID%20
which is not being recognized as valid.
JavaScript
1
4
1
var url = "http://api.openweathermap.org/data/2.5/weather?" +
2
"zip=" + zip +
3
"&APPID=" + APPID;
4