0

Im having a problem in getting the json result on one of my api's:

Tried calling it using the code below:

var url = 'domain.com/'
var query = $http({
    method: 'GET',
    url: url,
    headers: {'Content-Type': 'application/json'}
});
console.log(query);

and a simple $http get on my controller

var url = $http.get('http://domain.com');
console.log(url);

Always gave me the

XMLHttpRequest cannot load http://domain.com. Origin domain is not allowed by Access-Control-Allow-Origin.

Then I tried changing the method type from GET to JSONP but gave me a

Uncaught SyntaxError: Unexpected token :

Weird coz if you view the url using a web browser it is a valid json object.

Wondering Coder
  • 1,652
  • 9
  • 31
  • 51

1 Answers1

1

http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

You are violating the single origin policy by trying to XHR data from a domain external to the domain that delivered the invoking JS. Data retrieved with XHR must originate from the same host and port the site is served from. This is put in place to protect against cross site scripting attacks.

You can work around this with JSONP (I suspect there is a seperate issue with your JSONP call, post it?) or by including the correct headers in the response from your API, Access-Control-Allow-Origin: *;

Drew R
  • 2,988
  • 3
  • 19
  • 27