我在 localhost:3000 上運行服務器,而我的 React 應用程式在 localhost:8080 上。我查看了其他 Stack Overflow 問題,似乎設定完整 url 有效,但在我的情況下并非如此。
服務器.js
const sendAxie = require("./sendAxie");
const express = require("express");
const path = require("path");
const cors = require("cors");
const app = express();
const publicPath = path.join(__dirname, "..", "public");
app.use(cors());
app.use(express.static(publicPath));
app.post("/send-axie", (req, res) => {
// console.log(req.body);
console.log("in send-axie");
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("Server is up on " PORT);
});
應用程式.js
axios
.put("http://localhost:3000/send-axie", { axieID })
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log("in catch");
console.log(err);
});
安慰:
PUT http://localhost:3000/send-axie 404 (Not Found)
Error: Request failed with status code 404
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.onloadend (xhr.js:66)
uj5u.com熱心網友回復:
在您的快遞應用程式中,您已經撰寫了一個postapi。作為反應,您正在呼叫putapi。將其更改為post
uj5u.com熱心網友回復:
據我了解,您提出PUT請求,但僅限于您的路線POST。你需要添加這個
app.put("/send-axie", (req, res) => {
// console.log(req.body);
console.log("in send-axie");
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/349256.html
