【问题标题】:POST http://localhost:5000/getData 404 (Not Found) in reactjs nodejs axios [closed]在reactjs nodejs axios中发布http://localhost:5000/getData 404(未找到)[关闭]
【发布时间】:2021-10-18 03:36:29
【问题描述】:

我是 react 和 node.js 的新手。我正在尝试做的是我制作了一个假服务器(在端口 5000 上运行),其 API(http://localhost:5000/getData)具有对象的 harcode 值数组。现在我想从反应前端(在端口 3000 上运行)在 API(http://localhost:5000/getData)中添加新对象。为此,我正在使用 axios 的帖子,但是当我尝试添加新对象时,它给了我这样的错误:

有点困惑为什么它是这样的,我认为我做的一切都是正确的? 我附上相关代码。

server.js

const express = require("express");
const app = express();
const cors = require('cors')

app.use(cors({
    origin: 'http://localhost:3000'
}))


// Routes 
app.get('/getData', (req, res) => {
    res.json([
        {
            "description": "",
            "durationday": "fullday",
            "end": "null",
            "id": 0,
            "location": "",
            "start": "null",
            "title": "",
        },
        {
            "description": "yaaa!",
            "durationday": "absent",
            "end": "10-04-2021 12:10:00",
            "id": 1,
            "location": "perth",
            "start": "10-04-2021 12:10:00",
            "title": "holiday",
        },
        {
            "description": "busy",
            "durationday": "fullday",
            "end": "10-22-2021 12:10:00",
            "id": 2,
            "location": "melbourne",
            "start": '10-19-2021 12:00:00',
            "title": "working",
        },
        {
            "description": "dd",
            "durationday": "halfday",
            "end": "10-24-2021 12:10:00",
            "id": 3,
            "location": "updated perth",
            "start": "10-24-2021 12:10:00",
            "title": "updated holiday",
        }
    ]);
});



app.listen(5000, () => console.log("Running on pot 5000"))

反应 axios 函数

const sendData = async () => {
        const post = await axios.post('http://localhost:5000/getData', {

            description: "dd",
            durationday: "halfday",
            end: "10-24-2021 12:10:00",
            id: parseInt(344444),
            location: "updated perth",
            start: "10-24-2021 12:10:00",
            title: "updated holidayssssssssss",

        }
        )
            .then((response) => console.log(response.data))
            .catch((error) => console.log(error));

        return post;
    }

点击按钮

 <button onClick={sendData}> button post</button>

【问题讨论】:

    标签: javascript node.js reactjs axios httprequest


    【解决方案1】:

    您的路线设置为get,但您发送的是post

    app.get('/getData', (req, res) => {
    
    const post = await axios.post(
    

    【讨论】:

    • 嘿,谢谢你的回复,实际上我想在同一个 API 中添加新对象,所以我必须在我的 server.js 中创建另一个 API 吗?
    • 如果你想在同一个 url 处理 GET 和 POST 请求,你必须set it up to do that。你现在拥有它的方式是你的服务器只监听 GET 请求。
    • 感谢您的回复,现在我正在使用 axios.all 方法,它给了我响应 200 但它没有添加新对象。是不是因为我正在改变真实的对象?
    • 不确定你的意思。提供的服务器代码返回一个硬编码的数据数组。没有理由期望它会添加任何东西。
    • 好的,所以我猜想改变对象数组不是问题,但为什么它没有添加我试图通过 post 方法发送的新对象,知道吗?
    【解决方案2】:

    您通过GET 调用了getDate,但通过POST 调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2021-10-07
      • 2022-07-12
      • 2019-07-13
      • 2015-12-04
      • 2021-05-11
      • 1970-01-01
      相关资源
      最近更新 更多