admin管理员组文章数量:1401438
I was having an API where I am calling a get request to send to query parameters through form data but I am getting get request doesn't take form data. How to do it
(C)(C)Br&solvent=CC(C)(C)O
this is the URL and solute and solvent are the query parameters.
const onSubmit = (e) => {
e.preventDefault(); // <-- prevent the default form action
const formData = new FormData();
formData.set("solute", solutestate); // <-- local ponent state
formData.set("solvent", solventstate); // <-- local ponent state
console.log({ formData });
fetch(";, {
method: "get",
body: formData,
}).catch((err) => {
setError(err.error);
console.log(err);
});
When I tried with post
I got this error
I was having an API where I am calling a get request to send to query parameters through form data but I am getting get request doesn't take form data. How to do it
http://ihub-fastapi-solubility.herokuapp./predict?solute=CC(C)(C)Br&solvent=CC(C)(C)O
this is the URL and solute and solvent are the query parameters.
const onSubmit = (e) => {
e.preventDefault(); // <-- prevent the default form action
const formData = new FormData();
formData.set("solute", solutestate); // <-- local ponent state
formData.set("solvent", solventstate); // <-- local ponent state
console.log({ formData });
fetch("http://ihub-fastapi-solubility.herokuapp./predict", {
method: "get",
body: formData,
}).catch((err) => {
setError(err.error);
console.log(err);
});
When I tried with post
I got this error
- 1 You can't send body with GET request, only with POST. Change the API so that it accepts and processes POST requests – ComixRu Commented Feb 8, 2022 at 1:52
-
1
@ComixRu I don't think OP wants to send a POST request at all, they just want to capture their
FormData
params as query parameters – Phil Commented Feb 8, 2022 at 2:20 - related: stackoverflow./q/61829691 – djvg Commented Nov 3, 2022 at 9:31
2 Answers
Reset to default 4FormData isn't the best choice here as that is primarily used in constructing multipart/form-data
request payloads for use in POST
, PUT
and PATCH
requests.
Use URLSearchParams instead to easily construct query parameters and serialise it into the URL...
const fetch = function fakeFetch(url, init) {
console.log(init.method, url)
}
const solutestate = "CC(C)(C)Br"
const solventstate = "CC(C)(C)O"
const params = new URLSearchParams({
solute: solutestate,
solvent: solventstate
})
fetch(`http://ihub-fastapi-solubility.herokuapp./predict?${params}`, {
method: "GET"
})
Using URLSearchParams
has the added benefit of correctly URL encoding your data.
In addition to @phil's answer: If you've got a form already, you can use FormData
to initialize the URLSearchParams
.
For example, if you've got a form like this
<form method="get" action="" id="form">
...
<input type="submit">
</form>
you can submit it as follows
const form = document.getElementById("form");
form.addEventListener("submit", submitForm);
function submitForm(event) {
event.preventDefault();
const url = form.action + "?" + new URLSearchParams(new FormData(form));
fetch(url)...
}
本文标签: javascriptHow to send form data through get request in fetch apiStack Overflow
版权声明:本文标题:javascript - How to send form data through get request in fetch api - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744244987a2596967.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论