Aessandro
Aessandro

Reputation: 5761

Forecast API data access

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.

Upvotes: 4

Views: 2275

Answers (2)

onionpsy
onionpsy

Reputation: 1522

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);
  }
});

Upvotes: 10

Alex Weinstein
Alex Weinstein

Reputation: 9891

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.

Upvotes: 3

Related Questions