我想我有一個很好的問題。我正在制作我自己的總體請求存盤庫,我可以參考它而不是將所有請求嵌入到每個單獨的 JS 檔案中。
對于我傳遞身體的請求,我需要將兩件事傳遞給 stringify
- 識別符號表示的欄位
- 值表示內容
我似乎無法用其實際內容替換識別符號欄位,我認為它傳遞的是“欄位”作為其名稱而不是“欄位”字串的內容。
呼叫代碼:
Request.Request.useSendPostWithBody("User/checkAlt","email","[email protected]")
請求代碼:
Request.useSendPostWithBody = (endpoint, field, content) => {
const [ data, setData ]=useState('')
useEffect(() => {
fetch(`${window.ipAddress.ip}/${endpoint}`, {
method: "POST",
headers:{'Content-Type':'application/json'},
body: JSON.stringify({ email: content })}) // this works as the required field for this request is email
// body: JSON.stringify({ field : content })}) // this doesn't and here is where I think the issue lies because not all requests are going to pass "email"
.then((result)=> { return result.json() })
.catch(error =>{
console.log("error")
})
.then((data)=>{ setData(data)
console.log('data')
console.log(data) })
},[])
}
uj5u.com熱心網友回復:
您需要使用方括號[]來表示需要評估的鍵名,如下所示:
{
// other values,
body: JSON.stringify({ [field]: content })}),
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358973.html
標籤:javascript 反应钩子 拿来
上一篇:用遞回提取嵌套陣列的最后一個元素
下一篇:當彈出視窗打開時隱藏導航欄
