两个同时的 AJAX 请求不会并行运行 [英] Two simultaneous AJAX requests won't run in parallel
问题描述
我有两个同时运行的 AJAX 请求的问题.我有一个将数据导出到 XSLX 的 PHP 脚本.这个操作需要很多时间,所以我试图向用户展示进度.我正在使用 AJAX 和数据库方法.实际上,我很确定它曾经可以工作,但我不知道为什么,它不再在任何浏览器中工作.新浏览器有什么变化吗?
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() 启动会话时,它会打开文件进行写入并锁定它以防止并发编辑.这意味着对于使用会话通过 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屋!