试图找出FormData,但无法填充它。其他类似的问题是由于缺乏List<BrandMerchant>
元素的name
属性,但似乎并非如此。
HTML代码:
form
JS代码:
<form class="testForm" action="/database" method="put">
<input type="text" name="bar" value="foo">
</form>
<button type="button" class="testButt" name="testButt">click me!</button>
答案 0 :(得分:1)
尝试
console.log(document.forms[0].bar.value); // gets value from textbox.
创建您自己的表单数据:
var formData = new FormData(); // this is a *new* FormData object.
formData.append('foo', 'bar');
console.log(formData.get('foo'));
答案 1 :(得分:1)
数据实际上在那里,但是您不能通过使用console.log(formData)
输出对象来直接访问这些值。参见How to inspect FormData?。您还可以使用get()方法输出特定值。
以下是一个示例:
const testButt = document.querySelector('.testButt');
const testForm = document.querySelector(".testForm");
putDataTest = function() {
var xhr = new XMLHttpRequest();
var formData = new FormData(testForm);
formData.append("new", "foo");
console.log(formData.get('new'));
for (var pair of formData.entries()) {
console.log(pair[0] + ', ' + pair[1]);
}
xhr.open('PUT', '/database');
xhr.send(formData); // server sees req.body as empty
}
testButt.addEventListener("click", putDataTest);
<form class="testForm" action="/database" method="put">
<input type="text" name="foo" value="bar">
<input type="text" name="bar" value="foo">
</form>
<button type="button" class="testButt" name="testButt">click me!</button>