Tuesday, 13 August 2013

Any way to know if a XMLHttpRequest has completed within a seperate XMLHttpRequest scope?

Any way to know if a XMLHttpRequest has completed within a seperate
XMLHttpRequest scope?

any help with the following would be gratefully accepted:
I have a scenario whereby I have a variable number of Ajax file uploads on
a page, each contained within their own Form. During each forms' onSubmit
action I call a seperate method called PostFile() to perform the file
upload as follows:
function PostFile(fileInputId, id) {
var formdata = new FormData();
var fileInput = $('#fileinput' + '_' + id)[0];
formdata.append(fileInput.files[0].name, fileInput.files[0]);
formdata.append("Id", id);
//Creating an XMLHttpRequest and sending
var xhr = new XMLHttpRequest();
xhr.open('POST', '/Bookings/UploadArtwork');
xhr.send(formdata);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
if ($.ajax.active == 0) {
NoOtherFilesBeingUploaded();
}
}
};
}
My issue is that I only want the NoOtherFilesBeingUploaded() function to
run if there are no other files currently being uploaded via other forms
on the page; i.e. if there are no other XMLHttpRequest in progress.
Effectively I need a counter of current open XMLHttpRequests. However,
I've discovered that ajaxStart() does not fire for XMLHttpRequests events
so $.ajax.active is always 0.
Anybody have any ideas?
Thanks in advance

No comments:

Post a Comment