如何检查FormData? [英] How to inspect FormData?

查看:165
本文介绍了如何检查FormData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试 console.log 并使用 for 循环播放它。

I've tried console.log and looping through it using 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);
}

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

How can I inspect form data to see what keys have been set.

推荐答案

更新方法:

截至2016年3月,最近版本的Chrome和Firefox现在支持使用 FormData.entries()来检查FormData。 来源

As of March 2016, recent versions of Chrome and Firefox now support using FormData.entries() to inspect 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]); 
}

感谢 Ghost Echo rloth 指出了这一点!

旧答案:

查看这些 Mozilla 文章,看起来没有办法从FormData对象中获取数据。您只能使用它们来构建FormData以通过AJAX请求发送。

After looking at these Mozilla articles, it looks like there is no way to get data out of a FormData object. You can only use them for building FormData to send via an AJAX request.

我也刚刚发现这个问题说明了同样的事情: FormData.append(key,value)无效

I also just found this question that states the same thing: FormData.append("key", "value") is not working.

解决这个问题的方法之一就是建立一个常规字典,然后将其转换为FormData:

One way around this would be to build up a regular dictionary and then convert it to 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]);
}

如果你想调试普通的FormData对象,你也可以发送它为了在网络请求控制台中检查它:

If you want to debug a plain FormData object, you could also send it in order to examine it in the network request console:

var xhr = new XMLHttpRequest;
xhr.open('POST', '/', true);
xhr.send(fd);

这篇关于如何检查FormData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆