jQuery ajax async’true’让我的web-app卡住,直到返回数据

我正在研究基于PHP的Web应用程序(我没有构建)。

我正在运行这个ajax请求:

$.ajax({ type: 'POST', url: "/potato/ajax.php?module=test_module", dataType: 'json', async: true, data: { start_ts: that.start_date, stop_ts: that.end_date, submitted: true }, beforeSend: function() { console.log('Start: ' + new Date().toLocaleString()); // Show Chart Loading that.qwChart.showLoading({ color: '#00b0f0', // text: that.returnNumWithPrecent(that.progress) text: that.qwChartProgress }); // If data div isn't displayed if (!that.dataDisplayed) { // Show divs loading that.showMainDiv(); } else { that.$qwTbody.slideUp('fast'); that.$qwTbody.html(''); } }, complete: function(){}, success: function(result){ console.log('End: ' + new Date().toLocaleString()); // Clear timer clearInterval(timer); // Set progressbar to 100% that.setProgressBarTo100(); // Show Download Button that.downloadBtn.style.display = 'inline-block'; // Insert Chart Data that.insertChartData(result); // Insert Table Data that.insertTableData(result); } }); 

由于某种原因,它会让我的整个网络应用程序停滞不前,直到它返回数据。 我知道默认情况下ajax请求被设置为’true’但我还是添加了它,只是为了确保它是。 如果它是异步的,它应该完成工作而不会让我的网络应用卡住,我是对的吗? 可能是什么问题? 这是服务器端问题吗? 我该如何调试这种情况?

编辑:通过说“卡住”我的意思是 – 当我在提交ajax调用后等待响应时,刷新页面或并行打开其他页面(仅在我的Web应用程序中)显示白色加载屏幕。 每当ajax调用返回数据时 – 白页加载到请求的页面。

从PHP文件返回数据:

 <?php require_once("/www/common/api/db.php"); if (!empty($_POST['submitted'])) { // error_reporting(-1); // Users Array: $users = get_qw_data($start_ts_to_date, $stop_ts_to_date); // Summary Array: $summary = get_qw_summary($users); // QW Score Array: $qws = get_qw_score($users); // Generate CSV Report files /* Remove old:*/ if (!is_file_dir_exist($customer)) create_qw_directory($customer); /* Report #1: */ users_apps_google_macros_ma($users['users'], $customer); /* Report #2: */ usage_and_qw_summary($summary, $customer); /* Report #3: */ qw_score($qws, $customer); /* Zip Files: */ zip_qw_files($customer); echo json_encode($qws); } 

PHP会话是其他请求“卡住”的主要候选者,因为会话文件被写入锁定,因此只要一个正在运行的脚本实例打开会话,其他所有其他请求都必须等待。

解决方法是尽快调用session_write_close


一点延伸说明:

会话数据的默认存储机制就是文件系统。 对于每个活动会话,PHP只是将一个文件放入已配置的会话目录中,并将$ _SESSION的内容写入其中,以便在下一个需要访问它的请求时从那里读回它。

现在,如果几个PHP脚本实例试图“同时”将更改的会话数据写入该文件,那么这显然会产生很大的冲突/错误。

因此,只要一个脚本实例访问会话,PHP就会在会话文件上设置写锁定 – 其他所有人,其他请求(对同一个脚本,或者使用会话的另一个脚本)都必须等待,直到第一个脚本在会话中完成,写锁定再次释放。

默认情况下,脚本运行完毕时会发生这种情况。 但是如果你有更长的运行脚本,这很容易导致你遇到这种“阻塞”效果。 解决方法就是明确地告诉PHP(通过session_write_close ),“我已经在这里完成了会话,从这点开始就不会写任何新的/更改的数据 – 所以随意释放锁定,这样就可以了下一个脚本可以开始读取会话数据。“

重要的是,只有在脚本完成操作任何会话数据后才能执行此操作。 你仍然可以在脚本的其余部分读取 $ _SESSION – 但你不能再写它了。 (所以像$_SESSION['foo'] = 'bar'; ,在你发布会话后就必须失败。)

如果此时会话服务的唯一目的(在此特定脚本中)是检查用户身份validation,那么您可以在此之后直接关闭会话。 然后,脚本的其余部分可以根据需要运行,而不会阻止其他脚本再次访问同一会话。


这不仅限于AJAX请求 – 这些只是您通常首先注意到此类内容的地方之一,因为否则您通常不会使用在“并行”中运行的会话来获得那么多请求。 但是,如果您要在多个浏览器选项卡中多次打开一个长时间运行的脚本,您会注意到相同的效果 – 在第一个选项卡中脚本将运行并开展业务,而在以下选项卡中您应该注意到那些只要前一个脚本实例在会话上保持写锁定,请求也会“挂起”。