Home > Software engineering >  cannot post ; post can't be made with axis on react
cannot post ; post can't be made with axis on react

Time:11-14

I am trying to make a post request in React to the server

my React app is running at port 3000 & express app is running at port 9000

React:

  axios.post("/").then((response)=>{
    console.log(response.data)
  }).catch((e)=>{
    console.log(e.response.data)
    this.out = e.response.data
  })

Express:


app.post("/", (req, res) => {
    console.clear()
    console.log(req.body)
    res.end("req")
})

on the web console it says : "Failed to load resource: the server responded with a status of 404 (Not Found)"

on the app 'postman' It runs just fine

I tried to follow this YT tutorial https://www.youtube.com/watch?v=kJA9rDX7azM

CodePudding user response:

First you need to check weather you add a proxy in your React APP project Package.Json file under the private dependency name, proxy:

'http://localhost:9000/'

Axios first look for the port you are requesting for in the current server which is in you case is / so / is also a port in frontend so it never goes for the backend be safe to go with

axios.post("http://localhost:9000/")
.then((response)=>{
    console.log(response.data)
 })
.catch((e)=>{
    console.log(e.response.data)
})

Secondly make sure you must install the axios dependency in your react project

CodePudding user response:

Seems like you forgot to add domain in the axios request.

  axios.post("http://localhost:9000/").then((response)=>{
    console.log(response.data)
  }).catch((e)=>{
    console.log(e.response.data)
    this.out = e.response.data
  })

  • Related