duanshan188866 2014-11-11 10:10
浏览 345

在ajax回调函数的每个循环后追加输出

It's been more than 2 hours trying to find an optimal way to display the result rendered by an ajax call back function , Let me explain what i exactly want to do to better understand ...

I have a button "send" , on click event I defined a function that sends a $category_id to another page , where then I grab all the recipients from the database and send for each an Email. Now the whole process is working fine , but after each loop :

(foreach ($recipients as $recipient))
{send_mail();
echo "email sent successfully to ....";} 

I'm returning an echo statement telling that the email was correctly sent to that recipient . The Javascript function is defined to append those echo results in a div , but instead of displaying every statement after getting rendered , it gathers them all and display them at once .

$.ajax({
                async: true,
                url: 'send_emails.php?category='+category,
                success: function(r){
                    $('#status').append(r); //this happens all at once
                                    }
                }); 

Is there a better idea to reach my goal ?

thanks in advance

  • 写回答

2条回答 默认 最新

  • douwojiao5919 2014-11-11 10:18
    关注

    The Success event of an ajax called is triggered when the XHR status changes to 200 (successful). This only happens when the back-end is done processing.

    What you need is to check is the XHR readyState attribute. Which is set to '3' during processing of the request. You can then send updates from your PHP script to the webpage.

    Here is a good blog that explains how to do it

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记