Updated Method:
(更新方法:)
As of March 2016, recent versions of Chrome and Firefox now support using FormData.entries()
to inspect FormData.
(从2016年3月开始,Chrome和Firefox的最新版本现在支持使用FormData.entries()
检查FormData。)
Source .(来源 。)
// Create a test FormData object
var formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');
// Display the key/value pairs
for (var pair of formData.entries()) {
console.log(pair[0]+ ', ' + pair[1]);
}
Thanks to Ghost Echo and rloth for pointing this out!
(感谢Ghost Echo和rloth指出了这一点!)
Old Answer:
(旧答案:)
After looking at these Mozilla articles , it looks like there is no way to get data out of a FormData object.
(看完这些 Mozilla 文章之后 ,似乎无法从FormData对象中获取数据。)
You can only use them for building FormData to send via an AJAX request.(您只能将它们用于构建通过AJAX请求发送的FormData。)
I also just found this question that states the same thing: FormData.append("key", "value") is not working .
(我也刚刚发现这个问题指出了同样的问题: FormData.append(“ key”,“ value”)不起作用 。)
One way around this would be to build up a regular dictionary and then convert it to FormData:
(解决此问题的一种方法是建立一个常规词典,然后将其转换为FormData:)
var myFormData = {
key1: 300,
key2: 'hello world'
};
var fd = new FormData();
for (var key in myFormData) {
console.log(key, myFormData[key]);
fd.append(key, myFormData[key]);
}
If you want to debug a plain FormData object, you could also send it in order to examine it in the network request console:
(如果要调试普通的FormData对象,也可以发送它以便在网络请求控制台中检查它:)
var xhr = new XMLHttpRequest;
xhr.open('POST', '/', true);
xhr.send(fd);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…