简体   繁体   中英

How to get data from app.post in react.js?

//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();
  });
});

I want to access variable named "db" in App.js(React), but I don't know how to do. If I use axios like

//App.js

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

but "GET http://localhost:3000/trip 404 (Not Found)" prints. How can I solve this problem?

The /trip endpoint is of type post , you should be using axios.post() instead of axios.get() .

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

Refer https://axios-http.com/docs/post_example for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM