我使用Python EVE框架编写了一个API。当尝试从 AngularJS 应用程序访问 API 时,它显示错误,如下所示:
XMLHttpRequest cannot load http://127.0.0.1:5000/user/jay3dec. Request header field Authorization is not allowed by Access-Control-Allow-Headers.
为了纠正上述错误,我将以下内容添加到settings.py
X_DOMAINS = '*'
X_HEADERS = 'Authorization'
现在上述错误消失并在控制台中显示一个新错误:
XMLHttpRequest cannot load http://127.0.0.1:5000/user/jay3dec. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
但我已经添加了X_DOMAINS = '*'
在我的 settings.py 中。
这是 AngularJS 代码:
$scope.validate = function() {
var encodedUserNameAndPassword = Base64.encode($scope.username + ':' + $scope.password);
$http.defaults.headers.put = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': '*'
};
$http.defaults.headers.common['Authorization'] = 'Basic ' + encodedUserNameAndPassword;
$http({
method: 'GET',
url: 'http://127.0.0.1:5000/user/jay3dec'
}).
success(function(data, status, headers, config) {
console.log(data);
}).
error(function(data, status, headers, config) {
alert(data);
});
}
能发现可能是什么问题吗