[英]post request works with postman but not in browser, via axios
我正在開發MERN應用。 當我通過郵遞員向"/collections"
發出投遞請求時,得到了預期的響應,並且數據庫被新文檔更新。 當我在瀏覽器中發出請求時,代碼將運行,但是數據庫未獲取新文檔。 這是我的提交處理程序(reactjs):
handleSubmit = async e => {
e.preventDefault();
try {
await axios.post("/collections", this.state.data);
} catch (error) {
console.log(error.message);
}
this.props.history.push("/collections");
};
當我提交相關表單時,該代碼的最后一行運行,並且我的瀏覽器返回到"/collections"
。 該代碼不會引發或記錄任何錯誤,但是提交不會創建新文檔,正如mongodb Compass所揭示的那樣。 同樣奇怪的是,此代碼是從應用程序的另一部分修改而來的,該部分在瀏覽器中運行良好:
handleSubmit = async e => {
e.preventDefault();
await axios.post("/entries", this.state.data);
this.props.history.push("/entries");
};
這很好用,但是發布到"/collections"
卻沒有。 有什么幫助嗎?
作為參考,這是節點中的發布路線:
router.post("/", async (req, res) => {
try {
const newCollection = await Collection.create(req.body);
res.send(newCollection);
} catch (error) {
res.send(error.message);
}
});
感謝Pedro Brost,我弄清楚了。 在開發工具中的“網絡”下,由於貓鼬模式的設置方式,我收到驗證錯誤。 我不知道開發工具中是否包含“網絡”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.