0
我在Angular(1.4.0)中使用Chrome(43+)和Firefox(37+)後可以使用跨域POST請求,我在刪除Content-Type頭。角度跨域帖子:使用某些瀏覽器失敗
但在iPhone/iPad上使用Safari或Safari/Chrome時會失敗。然後 錯誤消息是:
未能加載資源:請求報頭字段的內容類型是不 由接入控制允許接頭允許的。
但在日誌中我可以看到:
[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);
});
}
});