两个同步AJAX请求将无法并行运行 [英] Two simultaneous AJAX requests won't run in parallel

查看:481
本文介绍了两个同步AJAX请求将无法并行运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,两个同时发生的AJAX请求运行。我有一个PHP脚本是将数据导出到XSLX。此操作需要大量的时间,所以我想,以显示进度给用户。我使用AJAX和数据库的方法。其实,我是pretty的确保其用于工作,但我想不出为什么,它不再在任何浏览器中工作。做了一些变化,新的浏览器?

I have problem with two simultaneous AJAX requests running. I have a PHP script which is exporting data to XSLX. This operation take a lot of time, so I'm trying to show progress to the user. I'm using AJAX and database approach. Actually, I'm pretty sure it used to work but I can't figure out why, it's no longer working in any browser. Did something change in new browsers?

$(document).ready(function() {

        $("#progressbar").progressbar();

        $.ajax({
            type: "POST",
            url: "{$BASE_URL}/export/project/ajaxExport",
            data: "type={$type}&progressUid={$progressUid}" // unique ID I'm using to track progress from database
        }).done(function(data) {
            $("#progressbar-box").hide();
            clearInterval(progressInterval);
        });

        progressInterval = setInterval(function() {
            $.ajax({
                type: "POST",
                url: "{$BASE_URL}/ajax/progressShow",
                data: "statusId={$progressUid}" // the same uinque ID
            }).done(function(data) {
                data = jQuery.parseJSON(data);
                $("#progressbar").progressbar({ value: parseInt(data.progress) });
                if (data.title) { $("#progressbar-title").text(data.title); }
            });
        }, 500);

    });

  • 进度正确更新数据库
  • JS​​的定时器正在试图获得的进展,我可以看到它在控制台中,但所有这些请求都加载一个脚本的整个期间,只要在脚本结束,这些AJAX进度调用加载

    那么,为什么第二AJAX调用等待第一个完成?

    So, why is the second AJAX call waiting for the first one to finish?

    推荐答案

    听起来像是一个会话阻塞问题

    Sounds like a session blocking issue

    在默认情况下PHP写的会话数据到一个文件中。当您启动与在session_start会话(),它会打开文件进行写入,并锁定至prevent并发编辑。这意味着,对每个请求使用会话经历一个PHP脚本必须等待第一次会议上的文件来完成。

    By default PHP writes its session data to a file. When you initiate a session with session_start() it opens the file for writing and locks it to prevent concurrent edits. That means that for each request going through a PHP script using a session has to wait for the first session to be done with the file.

    要解决这个问题的方法是更改​​PHP会话不使用的文件或关闭会话写像这样:

    The way to fix this is to change PHP sessions to not use files or to close your session write like so:

    <?php
        session_start(); // starting the session
    
        $_SESSION['foo'] = 'bar'; // Write data to the session if you want to
    
        session_write_close(); // close the session file and release the lock
    
        echo $_SESSION['foo']; // You can still read from the session.
    

    这篇关于两个同步AJAX请求将无法并行运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆