0

Sorry for duplicating a question for this issue but I really tried to seek for a solution but I could not find it. So I use angular $http.get xhr request to retrieve data from the public api. I have next code

 $http.get('http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getMakes&year=2000&sold_in_us=1')
     .success(function(d){ console.log(d); })
     .error(function(d){ console.log( "nope" ); 
 });

this returns in console:

XMLHttpRequest cannot load http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getMakes&year=2009. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'blah-my-localhost-blah' is therefore not allowed access. 

However if I user jquery getJson method it works just fine:

$.getJSON("http://www.carqueryapi.com/api/0.3/?callback=?", {cmd:"getMakes", year:"2009"},                      
  function(data) {
    console.log(data);
  });

So obviously I can configure angular to send correct request since jQuery works fine?

Thank you for the response.

2
  • Jquery getJSON is using jsonp since callback parameter exists in url Commented Sep 27, 2014 at 22:56
  • angular doesn't use ? for the callback the way jQuery does...read docs for proper callback string and use $http.jsonp docs.angularjs.org/api/ng/service/$http#jsonp Commented Sep 27, 2014 at 22:59

1 Answer 1

2

Use the $http.jsonp method to do a jsonp request along with JSON_CALLBACK as the callback name:

$http.jsonp('http://www.carqueryapi.com/api/0.3/?callback=JSON_CALLBACK&cmd=getMakes&year=2000&sold_in_us=1')
     .success(function(d){ console.log(d); })
     .error(function(d){ console.log( "nope" ); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

What does that strange callback=JSON_CALLBACK parameter do? Some explanation would be appreciated.

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.