Basicaly, I\'m performing an AJAX request for an external login system, how can I update the progress bar based on the length of the request?
For example, the reques
I think this post is quite clear http://www.dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5/
Posting this for future reference (should the blog be removed):
$.ajax({
xhr: function(){
var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
console.log(percentComplete);
}
}, false);
//Download progress
xhr.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},
type: 'POST',
url: "/",
data: {},
success: function(data){
//Do something success-ish
}
});
You can use jquery form plugin and use this method 'uploadProgress' like this:
$('#register-form').on('submit', function(e) {
e.preventDefault();
$(this).ajaxSubmit({
url: url,
uploadProgress: function (event, position, total, percentComplete){
$('.progress-bar').width(percentComplete + '%');
$('.progress-bar > p').html(percentComplete);
},
success: function (response, statusText, xhr, $form) {
// TODO: You'll need to do some custom logic here to handle a successful
// form post, and when the form is invalid with validation errors.
},
error: function(a, b, c) {
// NOTE: This callback is *not* called when the form is invalid.
// It is called when the browser is unable to initiate or complete the ajax submit.
// You will need to handle validation errors in the 'success' callback.
console.log(a, b, c);
}
});
});