【问题标题】:How to get data from app.post in react.js?react.js 如何从 app.post 获取数据?
【发布时间】:2023-01-08 16:06:45
【问题描述】:
//server.js

app.post('/trip', function(req,res){
  var params = "something";
  getResult(params).then((db)=>{
    // I want to access variable named "db" in App.js(React), but I don't know how to do.
    res.send(db);
    res.end();
  });
});

我想在 App.js(React) 中访问名为“db”的变量,但我不知道该怎么做。如果我像这样使用 axios

//App.js

axios.get('http://localhost:3000/trip').then((response)=>{
  console.log(response.data);
})

但是打印“GET http://localhost:3000/trip 404 (Not Found)”。我怎么解决这个问题?

【问题讨论】:

    标签: javascript node.js reactjs express


    【解决方案1】:

    /trip 端点的类型为post,您应该使用axios.post() 而不是axios.get()

    axios.post('http://localhost:3000/trip',#POST Call Body#)
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });
    

    有关更多详细信息,请参阅https://axios-http.com/docs/post_example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-27
      • 2021-04-15
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2018-02-11
      • 2020-07-11
      • 1970-01-01
      相关资源
      最近更新 更多