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

    评论

报告相同问题?

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。