最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing custom http response headers in angularjs - Stack Overflow

programmeradmin1浏览0评论

I am trying to access the header 'error-detail' as you can see in the browser network inspector (link above), the header gets returned. Server-wise I have also added the custom header to the 'Access-Control-Expose-Headers' to allow cross-domain requests as this was suggested to be the fix on other questions.

Below is the request to the server along with the success/error callbacks.

this.signon = function (request, onComplete, onError) {
    console.log("Calling server with 'login' request...");

    return $http.post("http://localhost:8080/markit-war/services/rest/UserService/login/", request)
        .then(onComplete, onError);
};

var onLookupComplete = function(response) {
    if (response.data.username)
    {
        //If user is returned, redirect to the dashboard.
        $location.path('/dashboard');
    }

    $scope.username = response.data.username;
};

var onError = function(response) {
    $scope.error = "Ooops, something went wrong..";
    console.log('error-detail: ' + response.headers('error-detail'));
};

When I try access the response header as seen below:

    console.log(response.headers());
    console.log('error-detail: ' + response.headers('error-detail'));

This only outputs: content-type: "application/json" error-detail: null

Is there a reason why the error-detail header is not being mapped over to the response object?

I am trying to access the header 'error-detail' as you can see in the browser network inspector (link above), the header gets returned. Server-wise I have also added the custom header to the 'Access-Control-Expose-Headers' to allow cross-domain requests as this was suggested to be the fix on other questions.

Below is the request to the server along with the success/error callbacks.

this.signon = function (request, onComplete, onError) {
    console.log("Calling server with 'login' request...");

    return $http.post("http://localhost:8080/markit-war/services/rest/UserService/login/", request)
        .then(onComplete, onError);
};

var onLookupComplete = function(response) {
    if (response.data.username)
    {
        //If user is returned, redirect to the dashboard.
        $location.path('/dashboard');
    }

    $scope.username = response.data.username;
};

var onError = function(response) {
    $scope.error = "Ooops, something went wrong..";
    console.log('error-detail: ' + response.headers('error-detail'));
};

When I try access the response header as seen below:

    console.log(response.headers());
    console.log('error-detail: ' + response.headers('error-detail'));

This only outputs: content-type: "application/json" error-detail: null

Is there a reason why the error-detail header is not being mapped over to the response object?

Share Improve this question edited Nov 28, 2016 at 21:37 Ibrahim Farah asked Nov 28, 2016 at 20:30 Ibrahim FarahIbrahim Farah 1331 silver badge7 bronze badges 5
  • can you do response.headers().error-detail ? – user2085143 Commented Nov 28, 2016 at 20:40
  • @user2085143 Nope, surely that isn't legal syntax is it? – Ibrahim Farah Commented Nov 28, 2016 at 20:45
  • Probably not, but not sure what response.headers() actually returns. Have you taken a look at this stackoverflow.com/questions/220231/… – user2085143 Commented Nov 28, 2016 at 20:56
  • Can you post the angular code that you are using to make the request, as well as the callback function? – Larry Turtis Commented Nov 28, 2016 at 21:16
  • @LarryTurtis Have edited my question to include the request + callback as requested. Thanks – Ibrahim Farah Commented Nov 28, 2016 at 21:38
Add a comment  | 

2 Answers 2

Reset to default 12

I think you are on the right track. To have access to custom headers, your server needs to set this special Access-Control-Expose-Headers header, otherwise your browser will only allow access to 6 predefined header values as listed in the Mozilla docs.

In your screenshot such a header is not present in the response. You should have a look at the backend for this cors header to also be present in the response.

This is a CORS Issue. Because this is a cross-origin request, the browser is hiding most ot the headers. The server needs to include a Access-Control-Expose-Headers header in its response.

The Access-Control-Expose-Headers1 response header indicates which headers can be exposed as part of the response by listing their names.

By default, only the 6 simple response headers are exposed:

  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

If you want clients to be able to access other headers, you have to list them using the Access-Control-Expose-Headers header.

For more information, see MDN HTTP Header -- Access-Control-Expose-Headers

发布评论

评论列表(0)

  1. 暂无评论