My code:
fetch("api/xxx", {
body: new FormData(document.getElementById("form")),
headers: {
"Content-Type": "application/x-www-form-urlencoded",
// "Content-Type": "multipart/form-data",
},
method: "post",
}
我尝试使用 fetch api 发布我的表单,它发送的正文如下:
-----------------------------114782935826962
Content-Disposition: form-data; name="email"
[email protected] /cdn-cgi/l/email-protection
-----------------------------114782935826962
Content-Disposition: form-data; name="password"
pw
-----------------------------114782935826962--
(不知道为什么boundary里的数字每次发送都会变……)
我希望它发送“Content-Type”:“application/x-www-form-urlencoded”的数据,我该怎么办?或者,如果我只需要处理它,如何解码控制器中的数据?
对于谁回答我的问题,我知道我可以这样做:
fetch("api/xxx", {
body: "[email protected] /cdn-cgi/l/email-protection&password=pw",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: "post",
}
我想要的是 jQuery 中的 $("#form").serialize() 之类的东西(不使用 jQuery)或在控制器中解码多部分/表单数据的方法。不过还是谢谢你的回答。