超過 3 個小時我正在處理一個問題,我正在嘗試使用 axios 庫從 react 向 nodejs 發送一個 get 請求,我想將一些資料傳遞到這個請求中,因為我們知道 get 請求不要'沒有正文,所以我將資料作為查詢引數發送
// base URL
const url = "http://localhost:8080/loginAsTeacher";
if(loginAs === "loginTeacher"){
axios.get(url,{
params :{
email: "[email protected]",
password: "abc1234*"
}
})
.then(res => console.log(res)) // this line return status:200 and data:null
.catch(err => console.log(err.message))
}
所以這個請求成功但問題是電子郵件和密碼沒有傳遞到后端
router.get("/loginAsTeacher", async (req,res)=>{
// values coming from the client
const loginEmail = req.params.email;
const loginPassword = req.params.password;
console.log(req.params); // this line return {} empty object
// get data of that user by his/her mail
const teacherData = await myModel.findOne({
email: loginEmail
}).exec()
res.status(200).json({
status: 200,
data: teacherData
})
})
上面的 console.log 回傳一個空物件,這意味著沒有引數
這不是正確的解決方案嗎???
謝謝閱讀
uj5u.com熱心網友回復:
要獲取您的查詢,您需要獲取 req.query 而不是 req.params
順便提一句。通過 get 發送敏感資料很危險。它甚至可以通過 https 以明文形式登錄
uj5u.com熱心網友回復:
使用 req.query 而不是 req.params。它將解決問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/405721.html
標籤:
