Access Control Allow Origin问题和angular$http服务
Access-Control-Allow-Origin issue and angular $http service
很抱歉为这个问题重复了一个问题,但我确实试图寻找解决方案,但找不到。所以我使用angular$http.get xhr请求从公共api中检索数据。我有下一个代码
$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" );
});
这在控制台中返回:
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.
然而,如果我使用jquery getJson方法,它工作得很好:
$.getJSON("http://www.carqueryapi.com/api/0.3/?callback=?", {cmd:"getMakes", year:"2009"},
function(data) {
console.log(data);
});
很明显,我可以配置angular来发送正确的请求,因为jQuery工作得很好?
感谢您的回复。
使用$http.jsonp方法执行jsonp请求,并将JSON_CALLBACK作为回调名称:
$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" ); });
相关文章:
- Angular http.get没有'我不喜欢在开头和结尾加方括号的文本文件
- 使用angular$http和django-urlpatters来获得json文件
- 从angular$http获取promise数据
- Angular$http然后解析_.each中的多个调用
- Angular $http 服务不加载 json 文件
- 如何使用Angular$http.jsonp从S3加载JSON文件
- Angular$http.jsonp-服务器don'不接受JSON_CALLBACK
- Angular-$http范围问题
- Angular$http.post未到达服务器
- Angular$http返回数据,但不返回'不适用于范围
- Angular $http not invoking
- Angular http在Phone gap中返回错误,但在浏览器中成功
- 为什么可以't我取消了这个Angular HTTP请求
- Angular$http$资源等价于PDF GET请求
- 在服务中使用Angular$http在ng点击时触发
- 如何使用$q's构造函数语法与Angular$http's配置超时
- Angular http Get Always 返回 ng-click 的状态 0
- 循环遍历 Angular HTTP 请求中的对象,等到响应后再运行下一个循环
- Angular http 返回 $$state 对象
- PHP 和 Angular $http ajax 的问题