Home > other >  Accessing JSON data through API with js
Accessing JSON data through API with js

Time:11-23

WHY! Why is this not working. I am using the weatherbit api, and trying to simply get the uv index. Mind you, in a browser the proper link displays all the information but I can not get it to work through js, putting it through to a div. I replaced the actual request data with (nameOfData) for privacy, I assure you that there is no problem with the parameters specific.

$getJSON('https://api.weatherbit.io/v2.0/current?lat=(Lat)&lon=(Lon)&key=(Key)', function(data) {
    // JSON result in `data` variable
    $(".UV").html(data.data[1].uv);
});

Please help.

CodePudding user response:

It is failing because you need an API key. Using the URL in the address bar of a browser does not have that restriction.

Sometimes a workaround in this case is to use a proxy on a server. Your code queries the server, and the server mimics a browser. It depends on the web service how you will fare.

CodePudding user response:

I got it working with async, somehow.


async function getData(){
let response = await fetch(weatherbitApi);
let data = await response.json()
return data;
}
getData().then (data =>
console.log(data.data[0]));

  • Related