API: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Avatar image for bentok
Bentok

4

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

Hey guys, any idea how I can resolve this error I get when I try to access the PI using angular? Here's my code:

http({method: 'GET', url: url, dataType: 'json'})

.then(function(response) {

scope.data = response.results;

console.log(scope.data);

}).catch(function(response){

console.log(response);

});

Then I get this error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:9000' is therefore not allowed access.

I'm running a node server with grunt on my local in this instance. Are there certain headers I need to be setting that I'm not aware of?

Thanks

Avatar image for szlifier
szlifier

1518

Forum Posts

120

Wiki Points

0

Followers

Reviews: 0

User Lists: 4

Yeah, if you're running this code in a browser it won't work. It's a cross-origin request and thus is forbidden by the browser, not the API.

The solution to this is jsonp, supported by the API.

However, this code should run on your node server.

Avatar image for bentok
Bentok

4

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

I was wanting to avoid jsonp if possible because Angular requires some annoying server side settings for it as well (the response needs to be wrapped in a callback function). I'll go with jquery or pure JavaScript then. Thanks for clearing that up for me!

Avatar image for bentok
Bentok

4

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

I found the solution here: http://www.giantbomb.com/forums/api-developers-3017/jsonpcallback-support-226006/

Turns out the URL parameter for format needed to be "jsonp" (duh) and the parameter for the callback key needed to be "json_callback." Got it working now!