[英]Access-Control-Allow-Origin issue and angular $http service
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.
因此,我使用角度$ http.get xhr请求从公共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: 但是,如果我使用jquery getJson方法,它就可以正常工作:
$.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? 显然,由于jQuery工作正常,我可以配置angular发送正确的请求吗?
Thank you for the response. 感谢您的答复。
Use the $http.jsonp method to do a jsonp request along with JSON_CALLBACK as the callback name: 使用$ http.jsonp方法与JSON_CALLBACK作为回调名称一起执行jsonp请求:
$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" ); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.