skip to Main Content

I’ve trying to create a progress bar while the script executes. It works fine when I test in Visual Studio, but when I put it on a server, it won’t work.
Is there some php or IIS setting that might be disabling this? It only fires at the end of the script execution.
I’ve also tried multiple different ways of doing the XHR request.
I’ve used xhrfields, xhr on it’s own and ya… again works in development, but on the server, nope.

My php file

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    header('Content-Type: application/json');
    $response = array();
    $links = array();
    $progress = 0;
    $progressFinal = 10;
    echo json_encode(array('progress' => $progress));
    ob_implicit_flush(true);
    ob_end_flush();
    for($i = 0; $i < 10; $i++) {
        sleep(2);
        $progress++;
        echo json_encode(array('progress' => (round(($progress / $progressFinal) * 100))));
        flush();
        ob_flush();
    }
}

my js file

$.ajax({
        url: "test.php",
        type: 'POST',
    dataType: 'json',
        async: true,
        xhrFields:
        {
            onprogress: function (e) {
                var thisResponse, response = e.currentTarget.response;
                if (lastResponseLen === false) {
                    thisResponse = response;
                    lastResponseLen = response.length;
                }
                else {
                    thisResponse = response.substring(lastResponseLen);
                    lastResponseLen = response.length;
                }

                jsonResponse = JSON.parse(thisResponse);
                $('#emrcp').html('- ' + jsonResponse.progress + '%');
            }
        },
        success: function (text) {
            $('#emrcBtn').html('Sent!');
            for (var i = 0; i < response.links.length; i++) {
                $('#emlStat' + response.links[i].fam_id).html('Sent...');
            }
        }
    });

2

Answers


  1. Chosen as BEST ANSWER

    It happened to be my IIS settings like I figured. I just need to add responseBufferLimit="0" to my webconfig for php and FastCGI.


  2. //first convert your js file into php file like js.php
    //and include it in your project
    
    <?php include "js.php"; ?>
    
    //like this
    //then
    //code this in js.php file
    
    let base_url = "<?= "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>";
    
    //then use it in ajax url like this 
    
    
    $.ajax({
            url: base_url+"test.php",
            type: 'POST',
        dataType: 'json',
            async: true,
            xhrFields:
            {
               // your code
            },
            success: function (text) {
             // your code
            }
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search