weixin_33744854 2014-07-18 15:09 采纳率: 0%
浏览 24

jQuery,ajax .done被解雇了吗?

did anyone have an idea why the: success, complete and done event fired 5-10 seconds later than the "xhr.upload.addEventListener("load")" event?

What is the correct event? iam not shure, which time the correct upload-time is?

hope, that somebody can help me :)

greets paD

$('body').on('change', '#fileUploader', function() {
// Post-Daten vorbereiten
//var data = new FormData();
//data.append('file', this.files[0]);

var xhr = new XMLHttpRequest();
var data = new FormData();

var files = $("#fileUploader").get(0).files;
for (var i = 0; i < files.length; i++) {
    data.append(files[i].name, files[i]);
}

// Ajax-Call
$.ajax({
    url: '<?=$this->language->modulLink('Upload/DoUpload');?>',
    data: data,
    type: 'POST',
    cache: false,
    processData: false,
    contentType: false,
    success: function() {
        console.log("JETZT");
    },
    complete: function(){
        console.log( "action finished" );
    },
    xhr: function() {
        var xhr = $.ajaxSettings.xhr();
        xhr.upload.addEventListener("progress", function(evt) {
            if (evt.lengthComputable) {
                var percentComplete = evt.loaded / evt.total;
                console.log(percentComplete + ' :: ' + evt.loaded + ' :: ' + evt.total);
                //Do something with upload progress here
            }
       }, false);

       xhr.upload.addEventListener("load", function(evt) {
            console.log("rdy");
        }, false);

       return xhr;
    },
}).done(function(d) { console.log(d); });

});

  • 写回答

1条回答 默认 最新

  • weixin_33701251 2014-07-18 15:18
    关注

    It's because you are making AJAX call which is asynchronous. Ajax sends the Request from the client side and without waiting for the response starts executing the code statements written(in your case event listener) after the AJAX call code statement. Then once it receives the response from server, based on the response it executes either the success/failure method.

    (1.) Ajax call is sent --> (2.) Statements after the AJAX call (i.e eventlistener method) are executed --> (3.) Server finally responds to AJAX call --> (4.) Success method of AJAX call is executed

    I recommend you to go through this tutorials. AJAX

    评论

报告相同问题?