javascript 如何从javascript中的FormData获取数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14876858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:49:40  来源:igfitidea点击:

How can i get data from FormData in javascript?

javascriptqunitform-data

提问by Taras Kravets

I need to read data from FormData? I try to read something like someFormatData["valueName"]but it not working. options["fileId"]or options["file"]does not work. Also I try options.fileIdsame result:

我需要从 FormData 读取数据吗?我尝试阅读类似的内容,someFormatData["valueName"]但它不起作用。 options["fileId"]options["file"]不起作用。我也尝试options.fileId相同的结果:

function upload(file, fileId, callback) {
    var formData = new FormData();
    formData.append("file", file);
    formData.append("fileID", fileId);

    $.ajax({
        url: '/url',
        type: 'POST',
        data: formData,
        processData: false,
        contentType: false,
        success: function(response) {
            callback(response);
        }
    });
}


asyncTest("test upload chunk", function() {
    var blob = new Blob(["Hello world!"], { type: "text/plain" }),        
        options = null,
        fileID ="someFileID",
        response;

    jQuery.ajax = function(param) {
        options = param;   // THIS is FormData object 
        // how to read fileId and file from here
    };

    upload(blob, fileID, function (data) {
        response = data;  
    });

    options.success({
        someProp: 'responseFromServer'
    });

    setTimeout(function() {
        QUnit.equal(options, "dataTosend", "parameters is OK");
        QUnit.equal(response["someProp"], "responseFromServer", "Response ok");
        start();
    },1000);
});

采纳答案by Kris Boyd

If you take your FormDataobject you can use a few different methods on it… What you are looking for is

如果你拿着你的FormData对象,你可以在它上面使用几种不同的方法......你正在寻找的是

formData.get()

or

或者

formData.getAll()

https://developer.mozilla.org/en-US/docs/Web/API/FormData

https://developer.mozilla.org/en-US/docs/Web/API/FormData

Note that the get()method is not fully supported on all browsers.

请注意,get()并非所有浏览器都完全支持该方法。