Home > Software engineering >  How to get data from app.post in react.js?
How to get data from app.post in react.js?

Time:01-08

//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?

CodePudding user response:

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.

  • Related