How do I close a connection early?

前端 未结 19 1397
情书的邮戳
情书的邮戳 2020-11-22 04:24

I\'m attempting to do an AJAX call (via JQuery) that will initiate a fairly long process. I\'d like the script to simply send a response indicating that the process has star

19条回答
  •  情深已故
    2020-11-22 04:58

    After trying many different solutions from this thread (after none of them worked for me), I've found solution on official PHP.net page:

    function sendResponse($response) {
        ob_end_clean();
        header("Connection: close\r\n");
        header("Content-Encoding: none\r\n");
        ignore_user_abort(true);
        ob_start();
    
        echo $response; // Actual response that will be sent to the user
    
        $size = ob_get_length();
        header("Content-Length: $size");
        ob_end_flush();
        flush();
        if (ob_get_contents()) {
            ob_end_clean();
        }
    }
    

提交回复
热议问题