XMLHTTPRequest事件侦听器不起作用?

时间:2016-04-08 17:38:34

标签: javascript jquery xmlhttprequest javascript-objects xmlhttprequest-states

由于某种原因,我的文件上传脚本无法正常工作,我完全不明白为什么:?该脚本设法发送文件,但我无法跟踪进度或状态更改。我试图注释掉不必要的位用于调试,但它仍然不起作用。

如果有人能指出我的错误,我将永远感激并给你一个虚拟饼干

Font font = new Font("Helvetica", Font.BOLD, 72);

3 个答案:

答案 0 :(得分:0)

尝试更改此内容:

xmlhttp.addEventListener('readystatechange', function(){
        if(this.readystate === 4){
            if(this.status === 200){
                //uploaded = JSON.parse(this.response);
                //if(typeof o.options.finished === 'function'){
                    o.options.finished();
                //}
            } else {
                //if(typeof o.options.error === 'function'){
                    o.options.error();
                //} 
            }   
        }
    });

对此:

xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readystate === 4){
            if(xmlhttp.status === 200){
                //uploaded = JSON.parse(this.response);
                //if(typeof o.options.finished === 'function'){
                    o.options.finished();
                //}
            } else {
                //if(typeof o.options.error === 'function'){
                    o.options.error();
                //} 
            }   
        }
    });

答案 1 :(得分:0)

我用'math.round()'代替'Math.round()'。一个非常简单的错误,但它阻止了脚本的其余部分执行。感谢Marvin Medeiros总是检查错误控制台的声音建议

var app = app || {};

(function(o){
    "use strict"

    var ajax, getFormData, setProgress;

    ajax = function(data){
        var xmlhttp = new XMLHttpRequest(), uploaded;
        xmlhttp.addEventListener('onreadystatechange', function(){
            if(xmlhttp.readystate === 4){
                if(xmlhttp.status === 200){
                    uploaded = JSON.parse(this.response);
                    if(typeof o.options.finished === 'function'){
                    o.options.finished();
                    }
                } else {
                    if(typeof o.options.error === 'function'){
                    o.options.error();
                    } 
                }   
            }
        });
        xmlhttp.upload.addEventListener('progress', function(event){
            var percent;
            if(event.lengthComputable === true){
                percent = Math.round(event.loaded / event.total * 100);
                setProgress(percent);
            }
        });
        xmlhttp.open('POST', o.options.processor);
        xmlhttp.send(data);
    }
    getFormData = function(src){
        var data = new FormData(), i;
        for(i=0;i<src.length;i++){
            data.append('file[]',src[i]);
        }
        data.append('formSent',true);
        return data;
    }
    setProgress = function(val){
        $('#pb').animate({width:val+"%"},10);
        $('#pt').html(val+"%");
    }

    o.uploader = function(opt){
        o.options = opt;
        if(o.options.files !== undefined){
            ajax(getFormData(o.options.files.files));
        }
    }
}(app));

答案 2 :(得分:0)

尝试一下:

xmlhttp.addEventListener('readystatechange', function(e){
        if(e.currentTarget.readyState === 4){
            if(e.currentTarget.status === 200){
               o.options.finished();
            } else {
               o.options.error();
            }   
        }
    });