我已经尝试了console.log和循环通过它使用for in。

这里是关于FormData的MDN参考。

两种尝试都在这把小提琴上。

var fd = new FormData(),
    key;

// poulate with dummy data
fd.append("key1", "alskdjflasj");
fd.append("key2", "alskdjflasj");

// does not do anything useful
console.log(fd);

// does not do anything useful   
for(key in fd) {
    console.log(key);
}

如何检查表单数据以查看已设置的键。


当前回答

console.log(myformdata.get("mykey");

我认为这就是答案! 你可以像这样使用"getall"

console.log(myformdata.getall());

其他回答

MDN建议采用以下形式:

let formData = new FormData();
formData.append('name', 'Alex Johnson')
for(let keyValuePair of formData.entries()){
    console.log(keyValuePair); //has form ['name','Alex Johnson']
}

另外

for (let [key, value] of formData.entries()) {
 console.log(key, ':', value);
}

考虑添加ES+ Polyfills,以防浏览器或环境不支持最新的JavaScript和FormData API。

我希望这能有所帮助。

你必须理解FormData::entries()返回一个Iterator实例。

以这个表单为例:

<form name="test" id="form-id">
    <label for="name">Name</label>
    <input name="name" id="name" type="text">
    <label for="pass">Password</label>
    <input name="pass" id="pass" type="text">
</form>

和这个js循环:

<script>
    var it = new FormData( document.getElementById('form-id') ).entries();
    var current = {};
    while ( ! current.done ) {
        current = it.next();
        console.info( current )
    }
</script>

下面是一个将FormData对象的条目作为对象记录到控制台的函数。

export const logFormData = (formData) => {
    const entries = formData.entries();
    const result = {};
    let next;
    let pair;
    while ((next = entries.next()) && next.done === false) {
        pair = next.value;
        result[pair[0]] = pair[1];
    }
    console.log(result);
};

.entries()上的MDN文档

MDN文档。next()和。done

简单的方法

我在angular 8中使用了这段代码

var formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');

 formData.forEach((value,key) => {
    console.log(key+value)
     });

console.log(myformdata.get("mykey");

我认为这就是答案! 你可以像这样使用"getall"

console.log(myformdata.getall());