Angular.js说自定义HTTP响应头是空的

时间:2022-03-14 03:47:59

The callback function for the POST is returning null for my custom HTTP header X-Auth-Token. Chrome is showing the correct POST response headers, but Angular.js is not.

POST的回调函数为我的自定义HTTP标头X-Auth-Token返回null。 Chrome显示正确的POST响应标头,但Angular.js不是。

The only ones Angular is returning are Cache-Control and Content-Type. Everything else shows null.

Angular唯一返回的是Cache-Control和Content-Type。其他一切都显示为null。

This is my CoffeeScript showing how I'm calling it:

这是我的CoffeeScript显示我是如何调用它的:

.factory 'loginFactory', ($rootScope, $http, $resource) ->
    $resource '/api/auth/login',
        email: '@id'
        password: '@id'

.controller 'userController', ($scope, $state, $http, loginFactory, userService) ->
    $scope.validationError = false
    $scope.user =
        email: ''
        password: ''

    $scope.loginUser = ->
        loginFactory.save $scope.user, (u, headers) ->
            console.log headers('X-Auth-Token')
        .$promise.then (response) ->
            unless response.error
                userService.login($scope.user.email, $scope.user.password)

                unless userService.redirIfLoggedIn()
                    $scope.validationError = true

I also tried running earlier versions Angular 1.3.x, and those had the same issue.

我也试过运行早期版本的Angular 1.3.x,那些也有同样的问题。

Why is Angular only returning those two headers when I make a request?

为什么Angular在我提出请求时只返回这两个标题?

1 个解决方案

#1


5  

Thanks for this solution goes to @dbugger who commented the answer I needed:

感谢这个解决方案转到@dbugger,他评论了我需要的答案:

Looks like the server needs to give permission to see the headers. Check out this: Reading response headers when using $http of Angularjs

看起来服务器需要授予查看标头的权限。看看这个:使用$ http的Angularjs时读取响应头

The proper way to make headers available outside of your local domain is to set: Access-Control-Expose-Headers on your webserver. In this particular case, you would put X-Auth-Token.

在本地域之外使标头可用的正确方法是在Web服务器上设置:Access-Control-Expose-Headers。在这种特殊情况下,您可以使用X-Auth-Token。

#1


5  

Thanks for this solution goes to @dbugger who commented the answer I needed:

感谢这个解决方案转到@dbugger,他评论了我需要的答案:

Looks like the server needs to give permission to see the headers. Check out this: Reading response headers when using $http of Angularjs

看起来服务器需要授予查看标头的权限。看看这个:使用$ http的Angularjs时读取响应头

The proper way to make headers available outside of your local domain is to set: Access-Control-Expose-Headers on your webserver. In this particular case, you would put X-Auth-Token.

在本地域之外使标头可用的正确方法是在Web服务器上设置:Access-Control-Expose-Headers。在这种特殊情况下,您可以使用X-Auth-Token。