我正在嘗試使用物件中的值。我不需要使用物件的“關鍵”部分。另外,我需要從物件周圍洗掉大括號 { }。我該怎么做?
我正在制作一個將資料發送到后端的搜索欄,但我只需要使用物件的“值”部分。也許我應該為此使用查詢字串?
const handleClick = (e) => {
e.preventDefault() // stop page from refresh
if (setUserInput !== '') { // is searchbar isnt empty, then run the POST method to send
// data from {userInput} which is the searchbar
const options =
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({userInput}),
};
const response = fetch('http://localhost:3001/search', options);
console.log("search entered");
};
}
當我在后臺控制臺記錄它給我的回應時
`"{ userInput: 'lollol' }"
除此之外,我只需要“lollol”部分(lollol 只是一個示例,但在搜索欄中輸入的任何內容都將顯示而不是顯示)。像這樣:
const searchResult = req.body /// return lollol ?
不是
const searchResult = req.body /// return `"{ userInput: 'lollol' }" ?
我應該使用查詢字串來完成此操作,還是有辦法從物件中提取資料?
uj5u.com熱心網友回復:
您不需要對正文中的物件進行字串化。代替
body: JSON.stringify({userInput}),
和
body: {userInput:userInput},
然后你可以像req.body.userInput. 希望能幫助到你!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/463113.html
標籤:javascript 节点.js 反应 表示 后端
