2 回答
TA贡献1951条经验 获得超3个赞
将异步参数值更新为 false。如果 async 参数值为 false,则 send() 方法在收到响应之前不会返回。
xhttp.open("GET","emailvalidate.php?uemail="+email,false);
试试下面的代码它会工作。
function check_email(){
var email = document.getElementById("uemail").value;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
result = true; // create a variable result and by default make it true.
xhttp.onreadystatechange=function(){
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("alert").style.display="inline";
if(xhttp.responseText=="EMP"){
document.getElementById("alert").innerHTML="<br><span class='badge badge-pill badge-info'>fill out emai id</span>";
console.log("Email id empty return false");
result = false;
}
else if(xhttp.responseText=="OK"){
document.getElementById("alert").innerHTML="<br><span class='badge badge-pill badge-success' >welcome new user</span>";
//document.getElementById("submit-button").disabled = false;
console.log("New email id return true");
}
else if(xhttp.responseText=="NO"){
document.getElementById("alert").innerHTML="<br><span class='badge badge-pill badge-danger'>Email Already Exist</span>";
//document.getElementById("submit-button").disabled = true;
console.log("Email id already exsist return false");
result = false;
}
else{
document.getElementById("alert").innerHTML=xhttp.responseText;
//document.getElementById("submit-button").disabled = true;
console.log("Error fetching email id");
result = false;
}
}
};
xhttp.open("GET","emailvalidate.php?uemail="+email,false);
xhttp.send();
return result; //at last return the result.
}
TA贡献2021条经验 获得超8个赞
这check_email是一个异步函数。这就是为什么你没有得到返回结果。你必须调用下面的函数。
function在关键字之前添加 async并在check_email function.
async function check_three_func() {
var check_email_func = await check_email();
var click_to_func = click_to();
var check_func = check();
if (check_email_func && click_to_func && check_func) {
return true;
console.log("all true");
}
else {
return false;
console.log(" false");
}
}
将您的 ajax 代码包装在一个 Promise 中,并根据响应解决或拒绝,然后返回该 Promise。
function check_email() {
return new Promise((resolve, reject) => {
var email = document.getElementById("uemail").value;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("alert").style.display = "inline";
if (xhttp.responseText == "EMP") {
document.getElementById("alert").innerHTML = "<br><span class='badge badge-pill badge-info'>fill out emai id</span>";
console.log("Email id empty return false");
reject(false);
}
else if (xhttp.responseText == "OK") {
document.getElementById("alert").innerHTML = "<br><span class='badge badge-pill badge-success' >welcome new user</span>";
//document.getElementById("submit-button").disabled = false;
console.log("New email id return true");
resolve(true);
}
else if (xhttp.responseText == "NO") {
document.getElementById("alert").innerHTML = "<br><span class='badge badge-pill badge-danger'>Email Already Exist</span>";
//document.getElementById("submit-button").disabled = true;
console.log("Email id already exsist return false");
reject(false);
}
else {
document.getElementById("alert").innerHTML = xhttp.responseText;
//document.getElementById("submit-button").disabled = true;
console.log("Error fetching email id");
reject(false);
}
}
};
xhttp.open("GET", "emailvalidate.php?uemail=" + email, true);
xhttp.send();
});
}
- 2 回答
- 0 关注
- 78 浏览
添加回答
举报