【发布时间】:2015-06-15 22:08:45
【问题描述】:
我在 Angular (1.4.0) 中有一个跨域 POST 请求,在我删除 Content-Type 标头后可以使用 Chrome (43+) 和 Firefox (37+)。
但在 iPhone/iPad 上使用 Safari 或 Safari/Chrome 时会失败。 然后错误消息是:
加载资源失败:请求头域 Content-Type 不是 Access-Control-Allow-Headers 允许。
但在日志中我可以看到:
[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);
});
}
});
【问题讨论】:
标签: javascript angularjs post cross-domain