我的代码:
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"
test@example.com
-----------------------------114782935826962
Content-Disposition: form-data; name="password"
pw
-----------------------------114782935826962--
(我不知道为什么每次发送的时候boundary里的数字都会变…)
我想用“Content-Type”:“application/x-www-form-urlencoded”发送数据,我该怎么做?或者如果我必须处理它,我如何解码控制器中的数据?
请回答我的问题,我知道我能做到:
fetch("api/xxx", {
body: "email=test@example.com&password=pw",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: "post",
}
我想要的是像$(“#form”).serialize()在jQuery (w/o使用jQuery)或解码控制器中的多部分/表单数据的方法。谢谢你的回答。
Content-Type: " multipart /form-data"
const formData = new FormData(document.getElementById("form"))
fetch("http://localhost:8000/auth/token", {
method: "POST",
body: formData,
headers: {
"Content-Type": "multipart/form-data"
}
})
Content-Type: "application/x-www-form-urlencoded"
const formData = new URLSearchParams(new FormData(document.getElementById("form")))
fetch("http://localhost:8000/auth/token", {
method: "POST",
body: formData,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
Content-Type: " multipart /form-data"
const formData = new FormData(document.getElementById("form"))
fetch("http://localhost:8000/auth/token", {
method: "POST",
body: formData,
headers: {
"Content-Type": "multipart/form-data"
}
})
Content-Type: "application/x-www-form-urlencoded"
const formData = new URLSearchParams(new FormData(document.getElementById("form")))
fetch("http://localhost:8000/auth/token", {
method: "POST",
body: formData,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})