在另一个AJAX调用中执行AJAX调用(没有jQUERY)

时间:2016-10-15 11:10:31

标签: javascript ajax

我正在使用第三方API将视频上传到该服务器。上传视频后,我会进行验证通话,以检查是否已上传所有文件(' verifyUpload()')。如果所有文件都已上传那么好,否则我将从上传的最后一个字节恢复上传(' resumeUpload()')。我遇到的问题,我不明白为什么,是我的' resumeUpload()'似乎没有执行 - 我编码错了吗?

在控制台中,我可以看到从resumeUpload()执行以下行:

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

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

但不是行: xmh.setRequestHeader(' Content-Range',stringArgument);

正在执行我看到: xmh.setRequestHeader(' Content-Range',' bytes / ');

从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();

}

1 个答案:

答案 0 :(得分:0)

我认为你在new错过了resumeUpload

var xmh = XMLHttpRequest;