我正在尝试通过
application/x-www-form-urlencoded
发送fetch
表格。
我的问题是发送数据的格式无效,如下所示:
"FormData {
[Symbol(state)]: [
{
name: '------WebKitFormBoundaryhPzR6LljgR6oqP3V\r\n' +
'Content-Disposition: form-data; name',
value: '"_id"\r\n' +
'\r\n' +
'2\r\n' +
'------WebKitFormBoundaryhPzR6LljgR6oqP3V\r\n' +
'Content-Disposition: form-data; name="zied"\r\n' +
'\r\n' +
'here I am\r\n' +
'------WebKitFormBoundaryhPzR6LljgR6oqP3V--\r\n'
}
]
}"
这是发送它的代码:
function startWithTemplate(event) {
const id = event.detail?._id;
const formData = new FormData();
formData.append('_id', id)
formData.append('zied', "here I am")
fetch('?/template', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formData
})
.then(response => {
console.log('Response:', response);
})
.catch(error => {
console.error('Error:', error);
});
}
于是我创建了一个
<form>
并尝试通过它发送数据,而且格式完全不同:
<form action='?/template' method='post'>
<input type='hidden' name='_id' value='2'>
<input type='hidden' name='zied' value='kidding'>
<button type='submit' >Send</button>
</form>
发送:
FormData {
[Symbol(state)]: [ { name: '_id', value: '2' }, { name: 'zied', value: 'kidding' } ]
}
检查负载源,我发现它是:
"_id=2&zied=kidding"
,所以我可以通过输入我的值而不使用 FromData
对象来解决这个问题,但我也希望能够处理更复杂的情况