我正在尝试使用XMLHttpRequest从另一个页面获取值,我发现只有在以下脚本中关闭所有警报后才会获取该值:
function changePw_prompt() {
var success = false;
while(!success) {
var newPassword = prompt("Please enter a new password", "");
if (newPassword != null) {
replyMsg = sendPassword(newPassword);
if (replyMsg == "CHNGSUCC") {
alert("Password changed");
sucess = true;
} else if (replyMsg == "CHNGFAIL") {
alert("Error: Password is shorter than 8 characters.");
sucess = false;
} else if (replyMsg == "CHNGERROR") {
alert("Internal error, please contact server admin.");
sucess = true;
}
} else {
alert("Password unchanged");
success = true;
}
}
}
function sendPassword(pwd) {
alert("Run 2nd function");
var xmlhttp2 = new XMLHttpRequest();
xmlhttp2.onreadystatechange = function() {
if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) {
var reply2 = xmlhttp2.responseText;
alert(reply2);
return reply2;
}
}
xmlhttp2.open("POST","changepw.php",true);
xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp2.send("uid="+document.getElementById("amsnr").value+"&pwd="+pwd);
}
我想知道这里有什么问题。另外,如果我想为replyMsg
变量返回正确的值,我应该如何更改我的代码,该变量从另一个页面获取响应。
感谢。
已更新我修改了我的代码以使其更简单,但它仍然无效,代码如下,我应该如何修改它以使用回调函数保持我的原始用途?
function changePw_prompt() {
var success = false;
while(!success) {
var replyMsg = "";
var newPassword = prompt("Please enter a new password", "");
if (newPassword != null) {
var xmlhttp2 = new XMLHttpRequest();
xmlhttp2.onreadystatechange = function() {
if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) {
var replyMsg = xmlhttp2.responseText;
alert(replyMsg);
if (replyMsg == "CHNGSUCC") {
alert("Password changed");
sucess = true;
} else if (replyMsg == "CHNGFAIL") {
alert("Error: Password is shorter than 8 characters.");
sucess = false;
} else if (replyMsg == "CHNGERROR") {
alert("Internal error, please contact server admin.");
sucess = true;
}
}
}
xmlhttp2.open("POST","changepw.php",true);
xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp2.send("uid="+document.getElementById("amsnr").value+"&pwd="+newPassword);
} else {
alert("Password unchanged");
success = true;
}
}
}
答案 0 :(得分:0)
示例:
/**
* Call Example
* changePw_prompt(function(changed) { console.log("password changed? ", changed); })
*/
/**
* @param function continueCallback : will run with the argument false/true
*/
function changePw_prompt(continueCallback) {
var newPassword = prompt("Please enter a new password", "");
if (newPassword == null) {
alert("Password unchanged");
continueCallback(false);
} else {
var xmlhttp2 = new XMLHttpRequest();
xmlhttp2.onreadystatechange = function() {
if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) {
var replyMsg = xmlhttp2.responseText;
alert(replyMsg);
if (replyMsg == "CHNGSUCC") {
alert("Password changed");
continueCallback(true);
} else if (replyMsg == "CHNGFAIL") {
alert("Error: Password is shorter than 8 characters.");
// repeat the process
changePw_prompt(continueCallback);
} else if (replyMsg == "CHNGERROR") {
alert("Internal error, please contact server admin.");
continueCallback(false);
}
}
}
xmlhttp2.open("POST","changepw.php",true);
xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var uid = encodeURIComponent(document.getElementById("amsnr").value);
var pwd = encodeURIComponent(newPassword);
xmlhttp2.send("uid="+uid+"&pwd="+pwd);
}
}