我有一個 API,我在其中呼叫 get 請求以通過表單資料發送到查詢引數,但我得到的 get 請求不接受表單資料。怎么做

http://ihub-fastapi-solubility.herokuapp.com/predict?solute=CC(C)(C)Br&solvent=CC(C)(C)O這是 URL,溶質和溶劑是查詢引數。
const onSubmit = (e) => {
e.preventDefault(); // <-- prevent the default form action
const formData = new FormData();
formData.set("solute", solutestate); // <-- local component state
formData.set("solvent", solventstate); // <-- local component state
console.log({ formData });
fetch("http://ihub-fastapi-solubility.herokuapp.com/predict", {
method: "get",
body: formData,
}).catch((err) => {
setError(err.error);
console.log(err);
});
當我嘗試使用時post出現此錯誤

uj5u.com熱心網友回復:
FormData在這里不是最佳選擇,因為它主要用于構建multipart/form-data請求有效負載以用于POST,PUT和PATCHrequests.
改用URLSearchParams來輕松構造查詢引數并將其序列化到 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.com/predict?${params}`, {
method: "GET"
})
使用URLSearchParams具有正確URL 編碼資料的額外好處。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/426775.html
標籤:javascript 反应 邮政 获取 API
上一篇:SlackAPI將字串上傳為檔案
