Javascript 逐个循环ajax请求并逐个显示结果?
我试图检查一些电子邮件地址是否注册在网站上或没有与旋度功能Javascript 逐个循环ajax请求并逐个显示结果?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图检查一些电子邮件地址是否注册在网站上或没有与旋度功能 function ProcessEmailList(mail_list) { // process one email at a time var value = mail_list.pop(); // if there is email in the array if (value) { $.ajax({ method: "POST", url: "valid_check.php",
function ProcessEmailList(mail_list) {
// process one email at a time
var value = mail_list.pop();
// if there is email in the array
if (value) {
$.ajax({
method: "POST",
url: "valid_check.php",
async: false,
data: {
email: value
}
}).done(function(data) {
if (data == "live") {
//append email to second textarea
} else {
//append email to third textarea
}
// recursion call, process rest of the email
ProcessEmailList(mail_list);
}).fail(function() {
console.log("error");
});
} else {
// no more email in the array, we have processed all
alert("All Request done!");
}
};
$("div#check").click(function() {
//Assume i just grab email lists from text area and put in array
var mail_lists_clean = ["a@yahoo.com", "b@aol.com", "c@gmail.com"];
ProcessEmailList(mail_lists_clean);
});
所以在本例中,我有3个textarea
元素。第一个包含电子邮件列表,第二个包含实时电子邮件(表示电子邮件已注册),第三个包含未注册的电子邮件。然后有一个名为“检查”的按钮,如果用户单击该按钮,它会一个接一个地发出AJAX请求,并一个接一个地显示结果,而不是一次(async:true
)中的多个AJAX请求,或者一个接一个AJAX请求,但在请求结束时显示一次结果(async:false
)
如果我使用async:true
我的浏览器会因大列表而崩溃,如果我使用async:false
我不知道我的应用程序是否正在运行,因为它会在请求结束时显示结果,即使请求是一个接一个的
那么为了让我的问题变得简单,这里是我的代码
$("div#check").click(function(){
//Assume i just grab email lists from text area and put in array
var mail_lists_clean = ["a@yahoo.com", "b@aol.com", "c@gmail.com"];
var promises = [];
$.each(mail_lists_clean, function(index, value){
var promise = $.ajax({
method: "POST",
url: "valid_check.php",
//cache: false,
async: false,
data: {
email: value
}
}).success(function(data) {
if (data == "live") {
//append email to second textarea
}
else {
//append email to third textarea
}
});
promises.push(promise);
});
$.when.apply($, promises)done(function() {
alert("All Request done!");
}).fail(function() {
console.log("error");
});
});
有人能帮我一个接一个地提出AJAX请求并一个接一个地显示结果吗?请更正我上面的代码。谢谢。逐一拨打ajax电话 您需要在
$处进行递归调用。ajax
success
回调函数
function ProcessEmailList(mail_list) {
// process one email at a time
var value = mail_list.pop();
// if there is email in the array
if (value) {
$.ajax({
method: "POST",
url: "valid_check.php",
async: false,
data: {
email: value
}
}).done(function(data) {
if (data == "live") {
//append email to second textarea
} else {
//append email to third textarea
}
// recursion call, process rest of the email
ProcessEmailList(mail_list);
}).fail(function() {
console.log("error");
});
} else {
// no more email in the array, we have processed all
alert("All Request done!");
}
};
$("div#check").click(function() {
//Assume i just grab email lists from text area and put in array
var mail_lists_clean = ["a@yahoo.com", "b@aol.com", "c@gmail.com"];
ProcessEmailList(mail_lists_clean);
});
为什么不在一个ajax请求中一次传递所有的EmailAddress,并将结果传回呢?这样长的地址列表就不会有问题了,它将为您省去很多麻烦,让您知道哪些请求得到了处理,哪些请求没有得到处理。这对性能也有好处。如果在使用大列表时崩溃,那么不要使用大列表,分页