0

ImageUpload.js

       axios.get('http://localhost:3001/encrypt' , { key : this.state.key}).then(response =>{
                console.log(response.data) ;
            }).catch(error=>{
            console.log(error);
        })

app.js

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app.get('/encrypt', (req, res) => {
    res.header("Access-Control-Allow-Origin", "http://localhost:3000")
    res.header("Access-Control-Allow-Headers")
    console.log(req.body) // it is returning {}
    res.send('hey nodejs')
});

Here, in app.js , req.body is returning an empty object due to which i am unable to access the key value from the object i sent during get request in 'ImageUpload.js' .

SBofficial
  • 193
  • 1
  • 10
  • Does this answer your question? [Send object with axios get request](https://stackoverflow.com/questions/46404051/send-object-with-axios-get-request) – Tanner Jan 29 '20 at 15:39
  • Or use `POST` method to pass request body. – Ihor Sakailiuk Jan 29 '20 at 15:40
  • if i use post method, it shows **error** like this. `Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3001/encrypt. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).` – SBofficial Jan 29 '20 at 16:00
  • If cors blocked, than you have to install `cors`: `npm install cors` and then set your cors in your middleware: `app.use(cors({ origin: '*'})); – Titus Sutio Fanpula Jan 29 '20 at 16:04

0 Answers0