ajax查询在窗口关闭后仍在等待

时间:2015-07-02 00:58:41

标签: javascript php ajax apache

我的网页有一个从php脚本获取数据的ajax调用。

ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("GET", "submit.php?id=" + id, true);
ajaxRequest.send(null);

在submit.php中,我等待二进制文件完成并创建一个outfile。

while(1){
    if(is_file($OUTFILEPATH)){
        break;
    }else{
        sleep(60);
    }
}

问题是如果用户关闭浏览器窗口,则不会中止ajax调用和相应的httpd。

如果用户多次刷新网页,我最终会有数百个httpd进程等待创建相同的outfile。

Apache Server Status
115 requests currently being processed, 5 idle workers

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW_W____........
................................................................
................................................................

Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,

1 个答案:

答案 0 :(得分:0)

我相信答案是我需要将数据发送到客户端(已关闭),这导致php意识到连接已关闭,然后你必须退出脚本。

while(1){
    if(is_file($OUTFILEPATH)){
        break;
    }else{
        sleep(60);
        print " ";
        ob_flush();
        flush();
        if(connection_aborted() != 0{
            exit;
        }
    }
}
相关问题