我遇到了一个非常奇怪的问题,我没有找到在线答案。
这是我的方法:
export const loginUser = (handleOrEmail, password) => {
const config = {
headers: {
"Content-Type": "application/json"
}
};
const body = JSON.stringify({ handleOrEmail, password });
try {
const { data } = await axios.post(
"http://localhost:5000/api/auth",
body,
config
);
console.log("data received!", data);
} catch (err) {
console.log(
"Err caught logging in!",
(err.response && err.response.data) || err
);
}
};
我的节点路线:
router.post("/", (req, res) => {
console.log("entered?");
const { handleOrEmail, password } = req.body;
return res.json({ msg: "backend reached!", handleOrEmail, password });
});
它打印entered
,但是req.body
未定义。为什么会这样呢? GET
请求有效(显然,POST请求也有效,但是主体是未定义的。)