简体   繁体   English

无法在 javascript onreadystatechange 中读取自定义 http 标头?

[英]Unable to read custom http headers in javascript onreadystatechange?

While i am trying to read custom http headers.i am hetting null.当我试图读取自定义 http headers.i 时,我正在为空。

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>

Headers :-标题:-

标题

在此处输入图片说明

Issue :-问题 :-

  I am unable read Authorization header content. 

I want to read the header value only in javascript.我只想在 javascript 中读取标头值。 Thanks in advance.提前致谢。

If someone still looking for the answer, you need to set "Access-Control-Expose-Headers" along side with the response.如果有人仍在寻找答案,您需要在响应旁边设置“Access-Control-Expose-Headers”。 In example :例如:

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

change your callback:改变你的回调:

xhr.onreadystatechange = function() {
      if(xhr.readyState == 4){//if you are looking only for HTTP 200, then add condition xhr.status == 200
         console.log(xhr.getResponseHeader("Authorization"));//will get your response header
         window.location.href = xhr.responseURL;//Redirect works
      }
  }

Note: readyState Holds the status of the XMLHttpRequest .注意: readyState保存XMLHttpRequest的状态。 Changes from 0 to 4:从 0 到 4 的变化:

0: request not initialized 0:请求未初始化

1: server connection established 1:服务器连接建立

2: request received 2:收到请求

3: processing request 3:处理请求

4: request finished and response is ready 4:请求完成,响应准备好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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