簡體   English   中英

角度跨網域發布:使用某些瀏覽器失敗

[英]Angular cross-domain post: fails using some browsers

我在Angular(1.4.0)中有一個跨域POST請求,在刪除Content-Type標頭后,它可以在Chrome(43+)和Firefox(37+)中使用。

但是在使用Safari或在iPhone / iPad上使用Safari / Chrome時失敗。 錯誤消息是:

無法加載資源:Access-Control-Allow-Headers不允許請求標頭字段Content-Type。

但是在日志中,我可以看到:

[Log] original httpProvider defaults (app.js, line 6)
[Log] Content-Type: application/json;charset=utf-8 (app.js, line 7)
[Log] updated httpProvider defaults (app.js, line 13)
[Log] Content-Type: undefined (app.js, line 14)
[Error] Failed to load resource: Request header field Content-Type is not allowed by Access-Control-Allow-Headers. (search, line 0)
...
headers: Object
  Accept: "application/json, text/plain, */*"

因此:即使標題中沒有Content-Type,也會出現錯誤消息。

有什么建議么?

片段:

var app = angular.module('myApp', []);

app.config(['$httpProvider', function($httpProvider) {
    console.log("original httpProvider defaults");
    console.log("Content-Type:", $httpProvider.defaults.headers.post['Content-Type']);

    //$httpProvider.defaults.useXDomain = true;
    //delete $httpProvider.defaults.headers.common["X-Requested-With"];
    delete $httpProvider.defaults.headers.post['Content-Type'];

    console.log("updated httpProvider defaults");
    console.log("Content-Type:", $httpProvider.defaults.headers.post['Content-Type']);
}]);

app.controller('testController', function($scope, $http, $sce) {
    console.log("testController");

    $scope.url = "http://.../v0/search";
    $scope.data = { "query":"auto" };

    $scope.testPost = function() {
        $http.post($scope.url, $scope.data).
            success(function(data, status, headers, config) {
                console.log("success");
                console.log(data, status, headers, config);
            }).
            error(function(data, status, headers, config) {
                console.log("error");
                console.log(data, status, headers, config);
            });
    }
});

抱歉打擾您了,畢竟這是服務器問題。 API提供程序已更新其API,現在可以使用了。

我仍然有點好奇,是什么導致服務器將同一個請求與不同的瀏覽器區別對待,並且當日志記錄告訴我未設置標頭字段時,我並不太理解錯誤消息。 但是它現在正在起作用,這才是最重要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM