Jota
Jota

Reputation: 865

Method Post with Axios giving Error 404. ReactJS

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?

Upvotes: 2

Views: 12372

Answers (2)

merrilj
merrilj

Reputation: 367

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.

Upvotes: 1

Arthur Almeida
Arthur Almeida

Reputation: 568

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?

Upvotes: 0

Related Questions