Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading