简体   繁体   中英

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("&nbsp;&nbsp;&nbsp;&nbsp; <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("&nbsp;&nbsp;&nbsp;&nbsp; <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("&nbsp;&nbsp;&nbsp;&nbsp; <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("&nbsp;&nbsp;&nbsp;&nbsp; <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 .

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. And then call that from inside the $.post callback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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