gpt4 book ai didi

javascript - 如何在处理时在ajax中附加基于php响应的消息?

转载 作者:太空宇宙 更新时间:2023-11-04 16:30:58 26 4
gpt4 key购买 nike

我正在尝试向多个人发送电子邮件,一旦电子邮件发送成功,我将通过 ajax 响应附加一条消息。下面的代码工作正常

PHP

if(!empty($sendEmails)){
$sendCount = 0;
foreach($sendEmails as $mail){
$sent = $this->sendEmail('', $mail, $subject, $data);
if($sent != 'Message Sent'){
$response['send_error'][] = 'There is an error with ['.$mail.']';
}else{
$sendCount++;
}
}
}
$response['success'] = 'Email has been sent successfully to '. $sendCount. ' person(s).';

Ajax

$.ajax({
type:'POST',
url:'post.php',
data:formData,
dataType:"json",
success:function(response){
$(".sndMail").remove();
if(response.success){
$(".sendResponse").prepend('<div class="alert alert-success sndMail"> <strong>Alert! </strong> '+response.success+'</div>');
}
if(response.send_error){
for(f=0; f<response.send_error.length; f++) {
//alert(response.fileErr[f]);
$(".sendResponse").prepend('<div class="alert alert-danger sndMail"> <strong>Alert! </strong> '+response.send_error[f]+'</div>');
}
}
if(response.error){
for(f=0; f<response.error.length; f++) {
//alert(response.fileErr[f]);
$(".sendResponse").prepend('<div class="alert alert-danger sndMail"> <strong>Alert! </strong> '+response.error[f]+'</div>');
}
}
}
});

但我想通过添加每封电子邮件发送回复来使用户查看得更好。我想在处理时附加每条响应消息,例如 Response: Email is send to php_dev@gmai.com ,一旦所有电子邮件都发送完毕,我将显示已经运行的成功消息。

那么有人可以指导我这是否可以通过ajax实现吗?如果有人指导我,我将不胜感激。我搜索了但没有找到解决方案。

最佳答案

通过一次 ajax 调用无法实现您想要的效果,但可以通过多次调用来完成。

这个想法是使用一个 ajax 调用来运行该流程,并使用第二个调用来定期轮询进度。

检查这个问题:Multiple Response AJAX request

关于javascript - 如何在处理时在ajax中附加基于php响应的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39827257/

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