【问题标题】:Unable to read custom http headers in javascript onreadystatechange?无法在 javascript onreadystatechange 中读取自定义 http 标头?
【发布时间】:2016-03-30 13:40:50
【问题描述】:

当我尝试读取自定义 http 标头时。我正在尝试为空。

球衣认证资源:-

   @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();
   }
  }

登录 - 页面:-

   <! 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>

标题:-

问题:-

  I am unable read Authorization header content. 

我只想在 javascript 中读取标头值。提前致谢。

【问题讨论】:

  • 您需要等到标头已发送...测试xhr.readyState &gt;= 2 ...我还将在xhr.send()之前设置onreadystatechange函数
  • @Jaromanda X 我尝试了你的建议,即使它不起作用。
  • i tried your suggestion - 你对我含糊的建议做了什么? ... it does not work - 任何开发者控制台错误?
  • 开发者控制台没有错误。
  • 检查 xhr.readyState >= 3 .但它不起作用

标签: javascript ajax jersey-2.0


【解决方案1】:

如果有人仍在寻找答案,您需要在响应旁边设置“Access-Control-Expose-Headers”。例如:

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

【讨论】:

    【解决方案2】:

    改变你的回调:

    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
          }
      }
    

    注意: readyState 保持XMLHttpRequest 的状态。从 0 到 4 的变化:

    0:请求未初始化

    1:建立服务器连接

    2:收到请求

    3:处理请求

    4:请求完成,响应就绪

    【讨论】:

    • readyState 的值从何而来? w3学校?根据mdn 的值是:0 = 未设置,1 = 打开(发送调用),2 = 收到标题(标题和状态可用),3 = 接收数据(responseText 将有部分内容),4 = 完成
    • 我试过上面你提到的代码。但我仍然是空的。
    • @Murugesan Era 你能否尝试获取其他标题,例如Content-Length,以确认问题仅与特定标题有关。通过评论window.location.href 尝试一下
    • @Tom Sebastian 我评论了它,我试过了,当时我也无法阅读标题内容。
    • @穆鲁格桑时代哦。其他标题也没有工作?
    猜你喜欢
    • 1970-01-01
    • 2018-04-09
    • 2017-12-10
    • 2020-04-01
    • 2013-02-09
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2010-10-09
    相关资源
    最近更新 更多