相关疑难解决方法(0)

AngularJS为跨源资源执行OPTIONS HTTP请求

我正在尝试设置AngularJS与跨源资源进行通信,其中传递我的模板文件的资产主机位于不同的域上,因此角度执行的XHR请求必须是跨域的.我已经为我的服务器添加了适当的CORS标头,以便使其工作,但它似乎不起作用.问题是,当我在浏览器(chrome)中检查HTTP请求时,发送到资产文件的请求是OPTIONS请求(它应该是GET请求).

我不确定这是AngularJS中的错误还是我需要配置一些东西.根据我的理解,XHR包装器无法发出OPTIONS HTTP请求,因此看起来浏览器正在试图确定在执行GET请求之前是否"允许"首先下载资产.如果是这种情况,那么我是否还需要使用资产主机设置CORS标头(Access-Control-Allow-Origin:http://asset.host ..)?

cross-domain cors angularjs preflight

262
推荐指数
8
解决办法
26万
查看次数

从jquery $ .ajax到angular $ http

我有这个jQuery代码,可以很好地交叉起源:

jQuery.ajax({
    url: "http://example.appspot.com/rest/app",
    type: "POST",
    data: JSON.stringify({"foo":"bar"}),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log("success");
    },
    error: function (response) {
        console.log("failed");
    }
});
Run Code Online (Sandbox Code Playgroud)

现在我想把它转换成Angular.js代码而没有任何成功:

$http({
    url: "http://example.appspot.com/rest/app",
    dataType: "json",
    method: "POST",
    data: JSON.stringify({"foo":"bar"}),
    headers: {
        "Content-Type": "application/json; charset=utf-8"
    }
}).success(function(response){
    $scope.response = response;
}).error(function(error){
    $scope.error = error;
});
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏.

ajax jquery cross-domain angularjs angular-http

121
推荐指数
1
解决办法
14万
查看次数

标签 统计

angularjs ×2

cross-domain ×2

ajax ×1

angular-http ×1

cors ×1

jquery ×1

preflight ×1