ajax percentage

by WILLIAM CORREA

HTML

<div id="progressCounter"></div>
<br>
<div id="loading">Loading</div>
<br>
<div id="data"></div>

JavaScript

var progressElem = $('#progressCounter');
var URL = "http://teste.fagoc.br/siga/script/size.php";
$("#loading").hide();
progressElem.text(URL);


$.ajax({
    type: 'GET',
    dataType: 'json',
    url: URL,
    cache: false,
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.responseText);
        alert(thrownError);
    },
    xhr: function () {
        var xhr = new window.XMLHttpRequest();
        //Download progress
        xhr.addEventListener("progress", function (evt) {
            console.log(evt.lengthComputable);
            if (evt.lengthComputable) {
                var percentComplete = evt.loaded / evt.total;
                progressElem.html(Math.round(percentComplete * 100) + "%");
            }
        }, false);
        return xhr;
    },
    beforeSend: function () {
        $('#loading').show();
    },
    complete: function () {
        $("#loading").hide();
    },
    success: function (json) {
        $("#data").html("data receieved");
    }
});