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.

2
  • JSON != JSONP. It doesn't look as if the web service you're using supports JSONP. Commented Aug 16, 2013 at 11:28
  • Hi check this url may be helpful to you stackoverflow.com/questions/16195199/… Commented Aug 16, 2013 at 12:02

1 Answer 1

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: *;

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, for now I made my own web service and added the header and it works!.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.