注意:我有两个 xmlHttp.open 请求,其中定义了两个不同的 url 字符串和两个 onreadystatechange 函数。我有一个包含 4 个文本框和一个提交按钮的 html 表单。用户在文本框 1(id) 和 2(type) 中输入数据后,我对这些框的 onchange 函数发出 xmlHttp.open("GET", url, true); 带有类似“getQuestion.php?id=12345&type=ABC”的网址。然后我的 statechange 函数获取返回的 xml 响应并将响应放入第三个文本框中。所有这些都很棒!我输入文本,文本弹出到一个框问我一个问题。
现在我尝试做的是,一旦填写了第 4 个文本框并单击了提交按钮,我希望提交函数再次使用 xmlHttp.open。这一次,有一个新的 url,在 &answer="XXXX" 的 url 行上多了一个参数,其中 XXXX 是在第 4 个框中输入的内容。之所以进行第二次 url 调用,是因为调用了新的 statechange 函数。但响应始终为空,我看到“答案是。”。我认为这一定与我没有正确设置的多个 url 有关,但我在“多个 xmlHttpRequest”的网络搜索中找不到任何内容。我附上了我的 HTML 和启动失败服务器请求的 javacode 块,它是 statechange 函数。我究竟做错了什么?还有一件事,
<script language="Javascript">
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
var attempts = 0;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
function CheckAnswer() {
var myId = document.getElementById("FilerID").value;
var myType = document.getElementById("FilerType").value;
var myQuestion = document.getElementById("QuestionID").value;
var myAnswer = document.getElementById("AnswerID").value;
if (myId == "" || myType == "" || myQuestion == "" || myAnswer == "")
{
alert("You must fill in all boxes before submitting this request.");
return;
}
// Build the URL to connect to
var url = "getAnswer.php?id=" + encodeURI(myId) + "&type=" + encodeURI(myType) + "&answer=" + encodeURI(myAnswer);
// Open a connection to the server
xmlHttp.open("GET", url, true);
xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
// Setup a function for the server to run when it's done
xmlHttp.onreadystatechange = AnswerState;
// Send the request
xmlHttp.send(null);
}
function AnswerState() {
if (xmlHttp.readyState == 4) {
var response2 = xmlHttp.responseText;
alert("answer was " + response2+".");
switch (response2)
{
case "pdwLIMBO":
alert("There was an error while trying to reset your password.");
break;
case "pdwFALSE":
alert("Answer was incorrect.");
attempts = attempts +1;
if (attempts == 3)
{
/*is there a way to kill it right now?*/
}
else
document.getElementById("AnswerID").focus();
break;
case "pdwTRUE":
alert("answer was " + response);
alert("Your new password will be emailed to you. Please check your email.");
break;
default:
alert("nothing!");
break;
}
}
}
<table width="98%" border="0" align="center" height="280" background="/images/DSCF0308_B.gif">
<tr>
<td align="center">
<h2 align="center" class="style26">Reset Service</h2>
<!--<form name="resetMe" action="SecretQuestion3.php" method="post" onsubmit="return CheckInput();">-->
<form >
<TABLE cellspacing=0 cellpadding=5 border=0 width="90%"> <!---this color is light blue in sign in box--->
<tr id="FilerIDRow">
<TD align="right" class="RptStd"><b>Filer ID:</b></TD>
<TD align="left" class="RptStd"> <input type="text" id="FilerID" name="FilerID" nowrap size=8/ onChange="callForQuestion();"></td>
</tr>
<tr >
<TD align="right" class="RptStd" ><b>Filer Type: </b></TD>
<TD align="left" class="RptStd">
<select name="FilerType" id="FilerType" onChange="callForQuestion();">
<option value="COH">COH: Candidate/Officeholder (Non-Judicial)</option>
<option value="JCOH">JCOH: Judicial Candidate/Officeholder </option>
</select></TD>
</tr>
<tr id="QuestionRow">
<TD align="right" class="RptStd"><b>Secret Question:</b></TD>
<TD align="left" class="RptStd"> <input type="text" id="QuestionID" name="QuestionID" nowrap size="50"></td>
</tr>
<tr id="AnswerRow">
<TD align="right" class="RptStd"><b>Your Answer:</b></TD>
<TD align="left" class="RptStd"> <input type="text" id="AnswerID" name="AnswerID" nowrap size="50" onChange="checkAnswer();"></td>
</tr>
<TR>
<TD align="center" colSpan=2><br><INPUT type="button" value=" Reset " name="btn_submit" class="RptBtn" onClick="CheckAnswer()">
<INPUT type="button" value=" Clear " name="btn_clear" class="RptBtn" onClick="ClearForm()"></TD>
</TR>
</table>
</form>
</td>
</tr>
</table>