我有一些 jQuery 来获取数据并执行回调:
$(function(){
$.ajax({
type: 'POST',
url: 'http://flash-cast-hrd.appspot.com/api/sonar/sync',
data: {
source: 5,
auth: 'JXEI....DHA2J'
},
success: displayData
});
});
我正在尝试从我的 Angular Controller 发出相同的请求,其中我在范围上有一个方法。
$scope.init = function(){
$http.post('http://flash-cast-hrd.appspot.com/api/sonar/sync', {
source: 5,
auth: 'JXEI....DHA2J'
})
.success(function(data){
console.log(data);
})
.error(function(error){
console.log('Error is: ' + error);
});
};
然后在 View 中调用它:
<div ng-init="init()"></div>
当我加载页面时,控制台中出现错误:
OPTIONS http://flash-cast-hrd.appspot.com/api/sonar/sync 405 (Method Not Allowed) angular.js:8021
OPTIONS http://flash-cast-hrd.appspot.com/api/sonar/sync No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. angular.js:8021
XMLHttpRequest cannot load http://flash-cast-hrd.appspot.com/api/sonar/sync. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. :8000/routes:1
Error is:
如何使 Angular $http.post 请求起作用?
最佳答案
问题是由于跨域访问造成的,您可以通过在服务器上设置 proxypass 来解决,并且您的请求还需要包含 Access-Control-Allow-header。
关于javascript - ajax 请求适用于 $ 但不适用于 Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21728260/