无法将变量值从 JS 发送到 Java Class
Unable to send variable value from JS to Java Class
我正在使用 Google 登录并获取用户电子邮件和令牌 ID。我正在捕获 JavaScript 函数中的值,这可以正常工作。这是使用的代码片段。我有一个需要此数据的 http servlet class。
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark" onSubmit=></div>
<script>
function onSignIn(googleUser) {
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
//send to backend
postIt(id_token);
};
function postIt(id_token) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
}
xhttp.open("POST","index.jsp", true );
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("id_token");
}
</script>
我的 Servlet 看起来像这样:
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id_token;
String email;
id_token=request.getParameter("id_token");
HttpSession session=request.getSession(true);
System.out.println("all is ok");
try {
System.out.print(id_token);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我在浏览器控制台中收到未捕获的 TypeError。我认为问题出在 postIt 函数中。
我无法理解如何将 id_token 值发送到 servlet。有人可以更正我的代码吗?
快速浏览后,我发现您的代码存在三个问题:
- 没有任何
#demo
元素 (或者您没有提供).
- 您还没有打开
XMLHttpRequest
到您的特定 class。
- 您不向服务器发送
id_token
,只发送参数。
代码:
/* Open the request to the servlet. */
xhttp.open("POST", "TestServlet", true);
/* Send the token to the server mapped to the 'id_token' parameter. */
xhttp.send("id_token=" + id_token);
我正在使用 Google 登录并获取用户电子邮件和令牌 ID。我正在捕获 JavaScript 函数中的值,这可以正常工作。这是使用的代码片段。我有一个需要此数据的 http servlet class。
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark" onSubmit=></div>
<script>
function onSignIn(googleUser) {
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
//send to backend
postIt(id_token);
};
function postIt(id_token) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
}
xhttp.open("POST","index.jsp", true );
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("id_token");
}
</script>
我的 Servlet 看起来像这样:
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id_token;
String email;
id_token=request.getParameter("id_token");
HttpSession session=request.getSession(true);
System.out.println("all is ok");
try {
System.out.print(id_token);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我在浏览器控制台中收到未捕获的 TypeError。我认为问题出在 postIt 函数中。 我无法理解如何将 id_token 值发送到 servlet。有人可以更正我的代码吗?
快速浏览后,我发现您的代码存在三个问题:
- 没有任何
#demo
元素 (或者您没有提供). - 您还没有打开
XMLHttpRequest
到您的特定 class。 - 您不向服务器发送
id_token
,只发送参数。
代码:
/* Open the request to the servlet. */
xhttp.open("POST", "TestServlet", true);
/* Send the token to the server mapped to the 'id_token' parameter. */
xhttp.send("id_token=" + id_token);