【问题标题】:ajax request works with $ but not angularajax 请求适用于 $ 但不是 angular
【发布时间】:2014-02-12 12:41:13
【问题描述】:

我有一些 jQuery 可以获取数据并执行回调:

$(function(){
$.ajax({
    type: 'POST',
    url: 'http://flash-cast-hrd.appspot.com/api/sonar/sync', 
    data: {
        source: 5,
        auth: 'JXEI....DHA2J'
    }, 
    success: displayData
});
});

我正在尝试从我的角度控制器发出相同的请求,我在范围上有一个方法。

$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);
                });
        };

然后在视图中调用它:

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

如何使有角度的 $http.post 请求工作?

【问题讨论】:

  • XMLHttpRequest错误可以通过部署在服务器上解决,部署到服务器后尝试运行,希望它可以工作
  • 访问控制允许来源
  • 尝试从服务器端处理

标签: javascript jquery ajax angularjs http-headers


【解决方案1】:

问题是由于跨域访问,您可以通过在服务器上设置 proxypass 来克服,并且您的请求还需要包含 Access-Control-Allow-header。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多