我啟動了一個全堆疊應用程式。我在后端部分撰寫了模型和路由器。在前端部分,我安裝了 axios 并將 "proxy": "http://localhost:xxxx/api" 添加到 package.json 檔案中。當我通過郵遞員發送獲取請求時,它會回應我的資料。但是當我轉到 http://localhost:xxxx 時,在 chrome 控制臺上顯示“AxiosError {message: 'Request failed with status code 404', name: 'AxiosError', code: 'ERR_BAD_REQUEST'”
它是我的 package.json 檔案的最后一行;
}, "proxy": "http://localhost:8800/api"
}
它是我的路由器;
router.get("/", async (req,res) => {
try {
const pins = await Pin.find();
res.status(200).json(pins);
} catch (err) {
res.status(500).json(err)
}
});
及其我的前端 app.js useEffect 代碼:
useEffect( () => {
const getPins = async () => {
try {
const res = await axios.get("/pins");
setPins(res.data);
} catch (err) {
console.log(err);
}
};
getPins();
}, []);
感謝您的幫助
uj5u.com熱心網友回復:
而不是嘗試 pingaxios.get("/pins")嘗試 ping axios.get("/")`
我相信您在路由上找不到 404,因為您的前端和后端路由不匹配。
uj5u.com熱心網友回復:
我重新啟動了 Visual Studio Code,問題就解決了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/476721.html
標籤:javascript 节点.js 反应 axios
