dave dave - 1 month ago 15
Ajax Question

Executing an AJAX call within another AJAX call (no jQUERY)

I am using a third party API to upload a video to that server. Once I have uploaded the video I make a verification call to check if all the file has been uploaded ('verifyUpload()'). If all the file has been uploaded then great, otherwise I resume the upload ('resumeUpload()') from the last byte that was uploaded. The problem that i am having, and I do not understand why, is that my 'resumeUpload()' does not appear to be executing - have i coded this incorrectly?

In the console I can see the following lines executing from resumeUpload():

console.log('vim.resumeUpload() ...');

console.log('stringArgument: ' + stringArgument);

But instead of the line:
xmh.setRequestHeader('Content-Range', stringArgument);

being executed I am seeing :
xmh.setRequestHeader('Content-Range', 'bytes /');

being executed from verifyUpload().

verifyUpload()

// VERIFY UPLOAD (SO FAR)
var verifyUpload = function(){
console.log('verifying upload() ...');

var xmh = new XMLHttpRequest;

xmh.onreadystatechange = function(){

console.log('xmh.readyState111: ' + xmh.readyState);
console.log('xmh.status111: ' + xmh.status);

if(xmh.readyState == xmh.HEADERS_RECEIVED){
console.log('VERIFY RESPONSE HEADERS: ' + xmh.getAllResponseHeaders());
console.log('getResponseHeader(\'Content-Range\')' + xmh.getResponseHeader("Range"));

var range = xmh.getResponseHeader("Range");
var rangeArray = range.split('-');
var bytesUploaded = rangeArray[1];
bytesUploaded = Number(bytesUploaded);

var leftToUpload = vim.vidFileSize-bytesUploaded;
console.log('bytesUploaded: '+bytesUploaded);
console.log('byteLeftToUpload: '+ leftToUpload);

// IF ALL THE FILE HAS BEEN UPLOADED TO THE SERVER
// COMPLETE UPLOAD
if(leftToUpload == 0){
completeVidUpload();
}else{
// NEED TO RESUME UPLOAD FROM WHERE WE LAST LEFT OFF
vim.bytesToUploadFrom = bytesUploaded + 1;
var stringValue = 'bytes '+vim.bytesToUploadFrom+'-'+vim.vidFileSize+'/'+vim.vidFileSize+'';
console.log('stringValue: '+ stringValue);
resumeUpload(stringValue);
}
}
}
xmh.open('PUT', vim.upload_link_secure);
xmh.setRequestHeader('Content-Range', 'bytes */*');
xmh.send();
}


resumeUpload():

// RESUME UPLOAD WHERE LEFT OFF
var resumeUpload = function(stringArgument){
console.log('vim.resumeUpload() ...');
console.log('stringArgument: ' + stringArgument);

var xmh = XMLHttpRequest;

// SET EVENT LISTENERS
// SET EVENT LISTENERS
xmh.upload.addEventListener('progress', uploadProgres, false);
xmh.addEventListener('load', uploadComplete, false);
xmh.addEventListener('error',uploadError, false);

xmh.onreadystatechange = function(){
if(xmh.readyState == xmh.HEADERS_RECEIVED){
console.log('VERIFY RESPONSE HEADERS222: ' + xmh.getAllResponseHeaders());
console.log('getResponseHeader(\'Content-Range\')222' + xmh.getResponseHeader("Range"));
}
}
xmh.open('PUT', vim.upload_link_secure);
xmh.setRequestHeader('Content-Range', stringArgument);
xmh.send();


}

Answer

I think you missed new in resumeUpload here:

var xmh = XMLHttpRequest;