我似乎无法发送$http.post
身份验证请求。我的代码与此页面AngularJS withCredentials上的解决方案相匹配,但我通过浏览器控制台收到此错误。
Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials.
这就是代码的样子:
$scope.authenticate = function (user, pass) {
$http.post(authUrl, {
username: user,
password: pass
}, {
withCredentials: true
})
}
我仍然是angularJS的新手,但我在另一个网站上寻求有关设置自定义标头的帮助,因此我可以修复有关标头的错误消息。我试着用:
$httpProvider.defaults.headers.post = { 'Access-Control-Allow-Credentials' : 'true' }
但如果没有$httpProvider undefined
错误,我似乎无法做到正确。我把它放在$http.post
调用之上,也在控制器的函数范围内:
.controller("authCtrl", function($scope, $http, authUrl, $httpProvider) {
有什么想法吗?
答案 0 :(得分:0)
From Angular's documentation,您只能在配置阶段与提供商进行互动。
app.config(["$httpProvider", function ($httpProvider) {
$httpProvider.defaults.withCredentials = true;
});
答案 1 :(得分:0)
Access-Control-Allow-Credentials
。进一步阅读:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials