When loading .php files through Ajax, I want to show how far the progress is rather than just the default spinner method $("#loader").css("display", "none");
.
Now as I have been building this, I have learnt that Chrome sends chunked data so the content-length parameter is not easily populated. There are a million questions on here about this as well.
So I am looking at the solution here from Nat and trying to set a header myself that will eventually contain the file size. I haven't even gotten to getting the full decompressed value to pass into the customer header (next bridge to cross) as I am trying to just pass a static value right now to check that the below JS works. You can see I am setting a value of 333 below. I have tried the same with headers:
as you can see commented out in the first line. Both methods set the headers but I dont get them when trying to log them into console to check why contentLength is always NaN.
FYI I have commented out //progressIndicator.update(e.loaded / contentLength);
below to avoid it crashing on error but will restore this part once I can actually get a byte value from the server.
$.ajax({
//headers: { 'x-decompressed-content-length': '333' },
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.addEventListener("progress", function (evt) {
var contentLength;
if (evt.lengthComputable) {
contentLength = evt.total;
} else {
console.log('all: ' + xhr.getAllResponseHeaders());
contentLength = parseInt(evt.target.getResponseHeader('x-decompressed-content-length'), 10);
}
//progressIndicator.update(e.loaded / contentLength);
});
return xhr;
},
beforeSend: function (xhr){
xhr.setRequestHeader('x-decompressed-content-length', '333');
},
url: cyberAjax.ajaxurl,
data: ({
action: 'ajax_load_tabs',
id: tab_id,
userid: userid,
}),
success: function(data){
$(tab_id).html(data);
},
error: function(data)
{
console.log("Error!");
return false;
}
});
Here you can see the header is set when I check the XHR requests in Network tab:
But when I use the console.log in the script above to retrieve all headers, there is no sign of them (and so my contentLength is always NaN):
Any thoughts?