[英]How do I print the successful statement in the if condition after getting values from an ajax call?
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").change(function(){
var p1 = $('#password1').val();
var p2 = $('#password2').val();
var p3 = $('#password3').val();
var p4 = $('#password4').val();
$.post('http://localhost../mycode.jsp',
{
pass1 : p1,
pass2 : p2,
pass3 : p3,
pass4 : p4,
},
function(data,status){
console.log(data)
alert("Status : "+ status);
});
});
});
</script>
</head>
<body>
<%
String pw1,pw2,pw3,pw4;
pw1 = "1";
pw2 = "2";
pw3 = "3";
pw4 = "4";
out.println("<div id='password' style=' display: block; position: absolute; left: 30%;'>");
out.println("<h1 style=' position: absolute; top: -120%; left: 18%; font-family: Titillium Web;'>PASSWORD</h1>");
out.println("<form method='POST'>");
out.println(" <input type='number' id='password1' maxlength='1' max='9' style=' width: 1%; padding: 12px 12px; margin: 8px 0px; border: none; border-bottom: 2px solid red;'>");
out.println(" <input type='number' id='password2' maxlength='1' max='9' style=' width: 1%; padding: 12px 12px; margin: 8px 0px; border: none; border-bottom: 2px solid red;'>");
out.println(" <input type='number' id='password3' maxlength='1' max='9' style=' width: 1%; padding: 12px 12px; margin: 8px 0px; border: none; border-bottom: 2px solid red;'>");
out.println(" <input type='number' id='password4' maxlength='1' max='9' style=' width: 1%; padding: 12px 12px; margin: 8px 0px; border: none; border-bottom: 2px solid red;'>");
out.println("</form>");
out.println("</div>");
String ele1 = request.getParameter("pass1");
String ele2 = request.getParameter("pass2");
String ele3 = request.getParameter("pass3");
String ele4 = request.getParameter("pass4");
if((pw1.equals(ele1)) && (pw2.equals(ele2)) &&(pw3.equals(ele3)) && (pw4.equals(ele4)))
out.println("Code Successful!!");
else
out.println("Code Failed.");
%>
</body>
</html>
This is the basic code of what I am working with. 这是我正在使用的基本代码。 I want to perform an action after the user form data is sent asynchronously to the server and server performs tasks.
我想在用户表单数据异步发送到服务器并且服务器执行任务之后执行操作。 How do I update those if condition values whose value is set at runtime by the user.
如果条件值的值由用户在运行时设置,我该如何更新那些值。
I want to perform an action after the user form data is sent asynchronously to the server and server performs tasks.
我想在用户表单数据异步发送到服务器并且服务器执行任务之后执行操作。
Assuming we are sticking to Jquery ajax, anything you want to do in response to an async request has to be done via callback function . 假设我们坚持使用Jquery ajax, 那么响应异步请求而要做的任何事情都必须通过回调函数完成 。
In other words: 换一种说法:
$.post('http://localhost../mycode.jsp',
// ... other arguments to ajax call
function(data,status){
// do stuff that requires waiting on async request HERE
});
Thats the only place where you know the request has completed with whatever status. 多数民众赞成在那唯一的地方,您知道请求已完成的任何状态。
You can move the if
logic into there or another one suggestion is to wrap up the logic you want executed into another smaller function. 您可以将
if
逻辑移到其中,或者另一个建议是将要执行的逻辑包装到另一个较小的函数中。 And then call that from inside the $.post
callback. 然后从
$.post
回调内部调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.