简体   繁体   中英

AngularJS can't read response HTTP Headers from NodeJS server

I am currently trying to make a Token-Based auth between a NodeJS server and AngularJS client.

In other words: Server generates token, sends to client in http header, client receives and sets that token to be sent in the next request (in the http header), when the request gets to the server it's checked if it contains a previously sent token. Repeat.

The problem is I'm not able to read the headers from AngularJS, even though I can see the header was successfully set in the response, through Chrome's console.

So far, I've got an AngularJS interceptor (What I've been told I should do) like this:

angular
    .module('injector', [])
    .factory('sessionInjector', function(){

      var injectedSession = {
          response: function(res) {

                console.log("RESPONSE: " + JSON.stringify(res.config.headers));

              return res;
          };

      return injectedSession;
});

And this is the output:

RESPONSE: {"Accept":"application/json, text/plain, */*"}

I've just started with AngularJS and I'm a little lost. Hope I didn't mess up something really basic.

Thank you.

When you hit a http request it looks something like below :-

$http.get("your URL",)
      .success(function (data,status, headers, config) {
        console.log(headers('Content-Encoding'));
      })
      .error(function (error) {
          canceller.reject(error);
      });
}

And you will receive response from server with other detail in callback, and you can headers like above.

I hope it will help you. Thanks

It seems that changing

response

for

'response'

made the trick.

I don't know why that did happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM