无法在javascript onreadystatechange中读取自定义http标头? [英] Unable to read custom http headers in javascript onreadystatechange?

查看:64
本文介绍了无法在javascript onreadystatechange中读取自定义http标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我正在尝试阅读自定义http标头。我正在寻找空。

While i am trying to read custom http headers.i am hetting null.

泽西岛认证资源: -

Jersey authentication resource :-

   @Path("/redirect")
   public class RedirectDemo {
   @POST
   @Consumes(MediaType. APPLICATION_JSON )
   public Response getRedirect(@Context ServletContext context,UserTO user) {
   UriBuilder builder = UriBuilder.fromPath(context.getContextPath());
   System. out .println("User name is:"+user.getUserName());
   System. out .println("Password is:"+user.getPassword());
   builder.path("/main.html");
   return Response
   .status(Response.Status. SEE_OTHER )
   .header(HttpHeaders. AUTHORIZATION ,"Response authorize")
   .header("Access-Control-Allow-Origin", "*")
   .header("Access-Control-Allow-Methods", "GET, POST,
   DELETE, PUT")
   .header("Access-Control-Expose-
   Headers",HttpHeaders. AUTHORIZATION )
   .header(HttpHeaders. LOCATION , builder.build())
   .build();
   }
  }

登录 - 页面: -

Login – page :-

   <! DOCTYPE html>
   <html>
   <head>
   <meta charset="UTF-8">  
   <title>Login</title>
   </head>
   <body>
   <div class="container-test">
   <h3>
    <strong>iDNS</strong>
   </h3>
   </div>
   <div class="container">
   <form class="form-signin" id="loginForm" name="loginForm"
   action="/JerseyPageRedirection/redirect/redirect" method="post"
   id="login_form">
   <div class="errmsg text-center"></div>
   <label for="inputEmail">Email address</label> <input
   type="email"
   id="emailId" class="form-control" name="emailId"<label
    type="password"
    placeholder="Email address" required autofocus> <br>
   for="inputPassword">Password</label> <input
   id="password" class="form-control" name="password"
   placeholder="Password" required> <br>
   <!-- id="login-submit" -->
   <button type="button" class="btn btn-lg btn-primary btn-block"
   onclick="doLogin()">Sign in</button>
   </form>
   </div>
   <script>
   function doLogin() {
   var userName = window
   .btoa(document.getElementById('emailId').value);
   var password = window
   .btoa(document.getElementById('password').value);
   var formData = JSON.stringify({
         userName : userName,
         password : password
     });
    var xhr = new XMLHttpRequest();
     xhr.open("POST", "/JerseyPageRedirection/redirect/redirect");
     xhr.setRequestHeader("Content-type", "application/json");
     xhr.send(formData);
     xhr.onreadystatechange = function() {
     console.log(xhr.getResponseHeader("Authorization"));//null
     window.location.href = xhr.responseURL;//Redirect works
     }
    }
    </script>
     </body>
     </html>

标题: -

< img src =https://i.stack.imgur.com/MmyFa.pngalt =在此输入图像说明>

问题: -

  I am unable read Authorization header content. 

我想只在javascript中读取标题值。在此先感谢。

I want to read the header value only in javascript. Thanks in advance.

推荐答案

如果有人仍在寻找答案,您需要设置Access-Control-Expose-Headers与反应一起。例如:

If someone still looking for the answer, you need to set "Access-Control-Expose-Headers" along side with the response. In example :

Access-Control-Expose-Headers: Content-Type,Authorization,X-RateLimit-Limit,X-RateLimit-Remaining,X-RateLimit-Reset

这篇关于无法在javascript onreadystatechange中读取自定义http标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆