2

i have a problem with axios.js, i trying to use the method POST but it give error 404 and i dont know what do, the error is:

> POST http://localhost:3000/api/match 404 (Not Found)
> createError.js:16 Uncaught (in promise) Error: Request failed with status code 404
    at createError (createError.js:16)
    at settle (settle.js:18)
    at XMLHttpRequest.handleLoad (xhr.js:77)

My code with post:

teste = () => {
  console.log('login clicked')
  let data = JSON.stringify({
    password: this.state.password,
    username: this.state.email
  })

  axios.post('api/match', data, {
    headers: {
      'Content-Type': 'application/json',
    }
  })
}

Same thing if i to use this:

axios.post('http://localhost:3001/api/match', data, {
  headers: {
    'Content-Type': 'application/json',
  }
})

My package.json:

"proxy": "http://localhost:3001/"

Someone know why be giving this error? ???? I look that it is giving the URL of the app React and not of the server..

Someone help me please?

2 Answers 2

1

Try changing api/match to /api/match in your first axios.post. A possible reason your second post request with the full URL isn't working might be a CORS issue. There's a great cors package you can add to your backend app with two lines of code.

Sign up to request clarification or add additional context in comments.

Comments

0

The error says, basically, you don't have any http server running on localhost:3000 with a route /api/match.

Are the http server running?

3 Comments

I mean, is this the server address or , as you said, the React app address? if you type in your browser localhost:3000, does it displays a response from the http server?
Not, it is of the server, of the React app is 3000
You have to use this syntax: axios.post('http://localhost:3001/api/match', data, { headers: { 'Content-Type': 'application/json', } }) and remove the proxy option from the package.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.