1

我采用了非犹太方法并使用了$.ajax()ajax 文件上传,因为目前我无法弄清楚“骨干”方式。图片已上传,ajax 请求以 JSON 格式发回新的主干模型,其中填充了新照片的文件名。但是模型没有触发change事件,所以我无法填充个人资料图片。请参阅success下面的函数。

    getFile:function(e){
        e.preventDefault();
        that = this;
        var file = e.target.files[0];
        name = file.name;
        size = file.size;
        type = file.type;
        var formData = new FormData($('#upload-child-pic-form')[0]);
        console.log(this,'currentUploadU',this.currentUpload);
        formData.append('child_id',this.currentUpload);
        $.ajax({
            url: '/children/upload/',
            type: 'POST',
            xhr: function() { 
            var myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){ 
                    myXhr.upload.addEventListener('progress',function(data){
                        $("#upload-child-pic-progress").val(data.position / data.totalSize * 100);
                    }, false); 
                }
                return myXhr;
            },
            //Ajax events
            beforeSend: function(data){
                $("#upload-child-pic-progress").removeClass('hide');
            },
            success: function(data){
                $("#upload-child-pic-progress").addClass('hide');
                $("#add-child-profile-pics-modal").modal('hide');
                var sentData = $.parseJSON(data);
                var thisChild = that.children.get(sentData.id);
                thisChild.set("photo",sentData.photo);
                thisChild.trigger('change');
            },
            error: function(data){
                console.log('error',data);
            },
            // Form data
            data: formData,
            //Options to tell jQuery not to process data or worry about content-type.
            cache: false,
            contentType: false,
            processData: false
        });
    },
4

1 回答 1

0

你在那个变量和其他变量之前缺少 var ...

拥有这些全局变量是一种威胁,它们可以在调用成功回调之前在此范围之外更改/覆盖。

that = this; //should be - 'var that = this;'
于 2013-08-20T06:39:21.773 回答