dpbe81245 2016-10-31 15:43
浏览 118
已采纳

当ajax查询正在进行时,jQuery .load不起作用

I have a web application that loads different content into the same div (.content) using either an ajax request or a .load() request.

A simplified version of the ajax request is:

    $(document).on('click', '.button1', function() {
        $.ajax({
        type: "POST",
        url: "/path/to/file/ajax.php",
        data: {
            'param1': 'xyz',
            'param2': '123',    
        },
        timeout: 10000,
        dataType: "json",
        success: function(data) {
            if (data.status == 'Success') {
                $('.content').html(data.result);
            } else if (data.status == 'Error'){
                console.log('Something went wrong')
            }
        },
        error: function(x, status, error) {
            if (status === "timeout") { 
               console.log('Request Timed Out');
            } 
        },
        }); 
    });

on another button I have:

    $(document).on('click', '.button2', function() {
          $('.content').load('/path/to/file/getContent.php?ref=foo');
    });

The problem is that if I click on button1, and then click on button 2 whilst the ajax request is still executing, nothing happens. (ie. getContent.php doesn't seem to return anything for anywhere between 15-30 seconds)

Clicking on button 2 returns the results instantly, but as soon as button1 is clicked and the ajax request is being processed, it seems to "stall" the entire web app until it's completed (or errors).

(I've also tried using the abort() method to cancel the ajax request, but the same problem persists, even when the abort is successful).

UPDATE See solution/Answer below

  • 写回答

1条回答 默认 最新

  • dongwen3093 2016-10-31 18:05
    关注

    Following on from the tip from @adeneo, I did some more research into session_start and ajax calls which led me to this article:

    http://konrness.com/php5/how-to-prevent-blocking-php-requests/

    The problem absolutely was to do with session_start locking the session file which causes back-end issues with multiple ajax requests. Following the advice in the article above, I made use of session_write_close() in my PHP and everything is working great.

    If it helps anyone else, I actually changed my code from session_start() to:

        if (session_status() == PHP_SESSION_NONE) {
           session_start();
           session_write_close();
        }
    

    This makes session variables readable, but no longer locks the session file causing ajax request delays. (It also checks to make sure session_start() hasn't already been called)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 易优eyoucms关于二级栏目调用的问题
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题