2

i have a problem, i trying do the method delete with Express, Axios and Reactbut they are giving error e not working...

My code with AXIOS.JS with REACT:

remove = (name, id) => {
  axios.delete('http://127.0.0.1:9000/people/' + id).
  then((response => id.splice(name, 1)))
}

My EXPRESS.JS:

app.delete('/people', function (req, res) {
  res.send('DELETE request to homepage');
});

And the errors:

OPTIONS http://127.0.0.1:9000/people/0 404 (Not Found)

Failed to load http://127.0.0.1:9000/people/0: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.

Uncaught (in promise) Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:87)

Someone help me please?

Thijs Kramer
  • 1,082
  • 8
  • 17
Jota
  • 819
  • 6
  • 24
  • 41
  • have you checked your server log? sometimes, if server crashes while serving the request (due to any error), it turns out to same kind of problem. – Nah Apr 23 '18 at 13:35
  • 1
    Did you solve this? I'd be interested in the answer. – geoidesic Jul 17 '19 at 22:32

2 Answers2

0

It looks like that you have to enable CORS in Express, take a look at this answer: How to allow CORS?.

Thijs Kramer
  • 1,082
  • 8
  • 17
  • Thank you for your answer, the problem of CORS was fixed :))) but others problems continue.. :((( – Jota Apr 23 '18 at 13:35
-1

It looks like you are trying to send ID as request parameter to your backed.

You'll have to

Change axios.delete('http://127.0.0.1:9000/people/' + id).

to axios.delete('http://127.0.0.1:9000/people/:' id).

And

app.delete('/people', function (req, res) {

To

 app.delete('/people/id', function (req, res) {

You also need to enable CORS on your server side

app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
Muhammad Usman
  • 10,039
  • 22
  • 39