gpt4 book ai didi

Javascript/AJAX 发送多个返回

转载 作者:行者123 更新时间:2023-12-02 15:13:57 24 4
gpt4 key购买 nike

一旦用户输入他/她的凭据并且凭据经过验证,我将尝试替换表行。这是对 servlet 的简单 AJAX 调用,发送凭据并返回一些特定信息(名字、姓氏、邮政编码等)。

我注意到对authenticate 的调用实际上返回了多次,第一对为空且“未定义”。我放入了一些代码来处理这个问题,然后删除了所有故障排除警报,并发现它根本不起作用。

在authenticate()返回之前发出警报,它按照我的预期工作。但是,如果我取消警报,它就会停止工作。

我没有太多使用 AJAX,并且无法充分定义问题以进行任何在线研究。我希望有人能够认识到我的问题,尽管我在描述问题方面做得很糟糕。

那么,我哪里出错了?

HTML:

<!DOCTYPE html>
<html>
<head>
<title>Training Daily Activity Report</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="stylesheets/dar.css">
<script src="js/DAR.js"></script>
<script src="js/jquery-1.10.2.min.js"></script>
<style type='text/css'>
html {
background: url(images/officebuilding.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
</style>
</head>
<body>
<div style='width: 100%;text-align: center;margin-top: 55px;'>
<div style='width: 90%;margin-right: auto; margin-left: auto;'>
<form id='fmDAR' action='DARServlet' method='post'>
<input type="hidden" id="formType" name="formType" value="" />
</form>
<table style='width: 100%'>
<tr>
<td colspan='2' class='firstBlock'>
<span style='font-size: 24px'>Daily Activity Report</span>
</td>
</tr>
<tr>
<td style='width: 15%;text-align: right;'>
<span style='padding-right: 5px;'>Categories worked on:</span>
</td>
<td style='width: 75%;'>
<textarea rows='3' style='width: 99%;' type='text' id='taCategories' name='taCategories' value=''></textarea>
</td>
</tr>
</table>
<br>
<table style='width: 100%;'>
<tr>
<td class="secondBlock">
<span>Trainee</span>
</td>
<td class="secondBlock">
<span>DTO/Trainer</span>
</td>
<td class="secondBlock">
<span>Phase</span>
</td>
<td class="secondBlock">
<span>DOT</span>
</td>
</tr>
<tr>
<td style='text-align: center;'>
<input style="width: 98%;" type="text" id="txtTrainee" name="txtTrainee" value="" />
</td>
<td style='text-align: center;'>
<input style='width: 98%;' type="text" id="txtTrainee" name="txtTrainee" value="" />
</td>
<td style='text-align: center;'>
<input style='width: 98%;' type="text" id="txtTrainee" name="txtTrainee" value="" />
</td>
<td style='text-align: center;'>
<input style='width: 98%;' type="text" id="txtTrainee" name="txtTrainee" value="" />
</td>
</tr>
</table>
<br>
<table style='width: 100%;'>
<tr>
<td class='thirdBlock'>
Trainee's Comments: What did you learn? What do you still need to know?
</td>
</tr>
<tr>
<td style='width: 100%;'>
<textarea style='width: 99%;' rows='8' id='taTraineeComments' name='taTraineeComments' value=''></textarea>
</td>
</tr>
</table>
<br>
<table style='width: 100%;'>
<tr>
<td class='fourthBlock'>
DTO Comments: What areas of trainee performance need improvement?
</td>
</tr>
<tr>
<td style='width: 100%;'>
<textarea style='width: 99%;' rows='8' id='taDTOComments' name='taDTOComments' value=''></textarea>
</td>
</tr>
</table>
<br>
<table style='width: 100%;'>
<thead>
<tr>
<td>&nbsp;</td>
<td>Logon ID</td>
<td>Password</td>
<td>&nbsp;</td>
</tr>
</thead>
<tbody>
<tr>
<td class='authentication' id='tdTraineeAuthentication'>
Trainee Authentication:
</td>
<td>
<input type='text' id='txtTraineeAuthLogon' name='txtTraineeAuthLogon' value='' />
</td>
<td>
<input type='password' id='txtTraineeAuthPW' name='txtTraineeAuthPW' value='' />
</td>
<td>
<button onclick='authenticateUser()'>Authenticate</button>
</td>
</tr>
<tr>
<td class='authentication'>
DTO Authentication:
</td>
<td>
<input type='text' id='txtDTOAuthLogon' name='txtDTOAuthLogon' value='' />
</td>
<td>
<input type='password' id='txtDTOAuthPW' name='txtDTOAuthPW' value='' />
</td>
<td>
<button onclick='authenticateDTO()'>Authenticate</button>
</td>
</tr>
<tr>
<td class='authentication'>
DTS Authentication:
</td>
<td>
<input type='text' id='txtDTSAuthLogon' name='txtDTSAuthLogon' value='' />
</td>
<td>
<input type='password' id='txtDTSAuthPW' name='txtDTSAuthPW' value='' />
</td>
<td>
<button onclick='authenticateDTS()'>Authenticate</button>
</td>
</tr>
</tbody>
</table>
</div>
<div style='text-align: center;margin-top: 20px;'>
<button style='height: 45px;' id='btnSave' onclick='saveDAR()'>Save Daily Activity Report</button>
</div>
</div>

</body>

JS:

function authenticateUser() {
var logon = document.getElementById("txtTraineeAuthLogon").value;
var pw = document.getElementById("txtTraineeAuthPW").value;
if (logon != null && pw != null) {
var tmp = authenticate(logon, pw);
var tmp2;
if (tmp != null) { //authenticate returns several results, the first few being "null" or "undefined"
if (tmp.toLowerCase() != 'undefined') {
tmp2 = tmp;
}
}
if (tmp2 != null) {
var tmpRow = " <tr>"
+ " <td class='authentication' id='trTraineeAuthentication'>"
+ " Trainee Authentication:"
+ " </td>"
+ " <td colspan='3' style=\"text-align: center;\">"
+ " Authenticated as " + tmp2 + ""
+ " </td>"
+ " </tr>";
$("td#tdTraineeAuthentication").parent().replaceWith(tmpRow);
}
}
}
function authenticateDTO() {
}
function authenticateDTS() {
}
function authenticate(logon, password) {
var returnString = null;
var givenName;
var sn;
var postalCode;
$.ajax({
url: 'DARServlet',
data: {
formType: 'authenticate', logon: logon, password: password
},
contentType: 'application/json',
mimeType: 'application/json',
success: function (responseText) {
$.each(responseText, function (key, value) {
if (key == "givenName") {
givenName = value;
} else if (key == "sn") {
sn = value;
} else if (key == "postalCode") {
postalCode = value;
}
});
returnString = givenName + " " + sn + ", Employee #: " + postalCode;
//alert(returnString);
},
error: function (request, status, error) {
alert("The credentials used cannot be authenticated. Please re-enter and try again.");
return null;
}
});
alert("returnString: " + returnString);
return returnString;
}

最佳答案

ajax对象的successerror属性是最终将执行的函数。请记住,AJAX 中的 A 最初代表异步,因此您的成功/错误函数不会在与您进行 AJAX 调用的函数相同的范围内运行。

您可以跳过各种障碍来尝试使异步代码与返回值的概念相匹配,但这种方式是疯狂的。如果您向前推进逻辑,那么您会更幸运,以便在成功/错误函数中做出决策。

这是一种不同的编程思维方式,但不是说 if(authenticate()){ do some;},它需要来自 authenticate() 的同步返回值,您需要遵循像 authenticate(doSomething); (其中 doSomething 是函数名称)这样的模式,在执行链中向前传递您的逻辑。

有关更好地处理 JavaScript 中的异步逻辑的方法的更多示例,您可以搜索 JavaScript“promise”。

关于Javascript/AJAX 发送多个返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34618438/

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