简体   繁体   English

从ajax调用获取值后,如何在if条件中打印成功语句?

[英]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 . 假设我们坚持使用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.

相关问题 验证通过php ajax调用成功后如何关闭javascript弹出框 - How do I close javascript popup box after validation is successful with php ajax call 在Django中获得成功的ajax响应后,如何启用“提交”按钮并更改其值? - How do I enable a Submit button and change it's value after getting a successful ajax response in Django? 成功调用 AJAX 后如何打开模态窗口? - How can I open a modal window after successful AJAX call? 成功获取后如何打印成功消息? - How do I print a success message after a successful fetch? 我如何使用javascript中的ajax调用将参数或ajax数据传递给python脚本并在成功时获得响应? - how do i pass parameter or ajax data to a python script using ajax call in javascript and get response if it is successful? ajax调用成功,但是php中没有值 - ajax call successful but no values in php Ajax调用成功,但是无法从$ _POST获取单选值 - Ajax call successful, but can't get radio values from $_POST 在Emberjs中成功进行Ajax调用后,如何加载ember模型? - How can I load ember model after successful ajax call in Emberjs? 如何获得成功的Ajax响应后动态创建表? - How do I create a table dynamically upon getting a successful ajax response? 如何通过使用Ajax从数据库获取json数据,以使用javascript将个案添加到switch语句中? - How do I add cases to a switch statement using javascript by getting json data from a database using ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM