Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forecast API data access

Tags:

json

jquery

api

How can I access the following data? At the moment I only want to play around with data so that I can better understand how it all works. I have never worked with APIs before, however I am familiar with the concept of JSON.

 $.getJSON( "https://api.forecast.io/forecast/APIKEY/40.463487,17.248535", function( data ) {
    console.log('here');
    console.log(data);
 });

I have tried this on my local and it returns: XMLHttpRequest cannot load https://api.forecast.io/forecast/APIKEY/40.463487,17.248535. Origin http://weathercast.com is not allowed by Access-Control-Allow-Origin.

All I need is the data.

like image 930
Aessandro Avatar asked Dec 11 '22 10:12

Aessandro


2 Answers

you can't do cross-domain AJAX query,

if you want to resolve this, you can use JSONP:

$.ajax({
  url: "https://api.forecast.io/forecast/APIKEY/40.463487,17.248535",
  dataType: "jsonp",
  success: function (data) {
      console.log('here');
      console.log(data);
  }
});
like image 55
onionpsy Avatar answered Jan 10 '23 10:01

onionpsy


The reason this isn't working is because of the cross-domain security policy that your browser has. You cannot issue JSON requests to other domains.

But! JSONP to the rescue! Does Forecast.io have a JSONP endpoint? If so, you're in luck. If not, you will need to implement a server-side proxy for the requests to another domain, and call that proxy from within your javascript.

like image 36
Alex Weinstein Avatar answered Jan 10 '23 11:01

Alex Weinstein