gpt4 book ai didi

javascript - 无法读取 JavaScript 中的 http header 值

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:07 25 4
gpt4 key购买 nike

在尝试在服务器上设置的 HTTP header 时,我无法在 java 脚本上访问。下面是我的代码。请帮助我解决这个问题。

Jersey :

@Path("/redirect")
public class RedirectDemo {

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(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();

}
}

Java 脚本:

        <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"
placeholder="Email address" required autofocus> <br> <label
for="inputPassword">Password</label> <input type="password"
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() {
if (xhr.readyState == 4)
if (xhr.status == 200)
var json_data = xhr.responseText;
window.location.href = xhr.responseURL;
}
}
</script>
</body>

谢谢,我需要读取java脚本上的 header 值。

最佳答案

参见:Accessing the web page's HTTP Headers in JavaScript

本文介绍了获取所有 header 的字符串表示形式以及命名 header 的值的方法:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

DOMString getAllResponseHeaders();
DOMString? getResponseHeader(DOMString header);

关于javascript - 无法读取 JavaScript 中的 http header 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34415691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com