0

在这里,我在获取和使用对象的属性时遇到问题,我不明白为什么我在最近几天被卡住了......我做了一个截图,可能看到了对象的属性和方法列表.

在这种情况下,我会转到“结果”属性。问题:当我执行“console.log (objet.result)”时,它返回“未定义”。

它确实是一个 XHR 对象。脚本上传文件,它主要使用这个插件:https ://github.com/blueimp/jQuery-File-Upload/wiki/API

并在脚本代码下方(查看“if(progress == 100)”行):

$(函数(){

var ul = $('#upload ul');

$('#drop a').click(function(){
    // Simulate a click on the file input button
    // to show the file browser dialog
    $(this).parent().find('input').click();
});

// Initialize the jQuery File Upload plugin
$('#upload').fileupload({

    // This element will accept file drag/drop uploading
    dropZone: $('#drop'),

    // This function is called when a file is added to the queue;
    // either via the browse button, or via drag/drop:
    add: function (e, data) {

        var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
            ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');

        // Append the file name and file size
        tpl.find('p').text(data.files[0].name)
                     .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

        // Add the HTML to the UL element
        data.context = tpl.appendTo(ul);

        // Initialize the knob plugin
        tpl.find('input').knob();

        // Listen for clicks on the cancel icon
        tpl.find('span').click(function(){

            if(tpl.hasClass('working')){
                jqXHR.abort();
            }

            tpl.fadeOut(function(){
                tpl.remove();
            });

        });

        // Automatically upload the file once it is added to the queue
        var jqXHR = data.submit();
    },

    progress: function(e, data){

        // Calculate the completion percentage of the upload
        var progress = parseInt(data.loaded / data.total * 100, 10);

        // Update the hidden input field and trigger a change
        // so that the jQuery knob plugin knows to update the dial
        data.context.find('input').val(progress).change();

        if(progress == 100){
            data.context.removeClass('working');

            //THE VALUE THAT I WANNA GET IS A PROPERTY OF THE DATA OBJECT 
        }
    },

    fail:function(e, data){
        // Something has gone wrong!
        data.context.addClass('error');
    }

});

// Prevent the default action when a file is dropped on the window
$(document).on('drop dragover', function (e) {
    e.preventDefault();
});

});

请问有人可以帮助我吗??

提前感谢您的帮助和美好的一周!

4

2 回答 2

0

我认为这是一个回应的问题。Javascript 无法评估响应(将其视为字符串)。

第一个解决方案->

var obj = eval('{' + res + '}');

第二种解决方案->

我们可以使用 var response = jQuery.parseJSON(res); 将结果解析为 json;

然后访问 response.result

于 2014-05-26T14:13:37.383 回答
0

最后,我解决了我的问题。

我没有使用良好的回调参考来获取我的数据。我把我的鼻子深入插件文档,我发现了另一种方法,而不是“进步”。如果我想获得“结果”值,我必须使用“完成”回调方法。

但是,我要感谢您的帮助。

有一个愉快的编码日!

迈克尔

于 2014-05-28T09:43:29.490 回答