Skip to content Skip to sidebar Skip to footer

How To Flush Php Output Buffer Properly?

I need PHP to stream output to Javascript but Javascript keeps the old responses and prints them like so... Console logs: [0]: Line to show. [0]: Line to show.[1]: Line to show. [

Solution 1:

responseText always contains the entire response from the server. When you use the progress event, it contains the accumulated response so far, not just the incremental string added to the response in the most recent flush from the server.

Save the length of the previous response text in a variable, and then on subsequent calls just print the substring after that.

var responseLen = 0;
$.ajax({
    url: "../controller/controller.php", 
    type: "POST",
    data: {operation: 'rxMode'},
    xhr: function(){
        var xhr = $.ajaxSettings.xhr();
        xhr.onprogress = function(e){
            console.log(e.currentTarget.responseText.substr(responseLen)); 
            responseLen = e.currentTarget.responseText.length;
        };
        console.log(xhr);
        return xhr;
    }
 });

Post a Comment for "How To Flush Php Output Buffer Properly?"