Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios post with proxy on create-react-app hosting server doesn't work

Tags:

reactjs

axios

I have following line in my package.json (react app)

"proxy": "http://www.demoapp.com"

I'm requesting data using REST-API call

axios.post('/user_data', { // http://www.demoapp.com/user_data
        id:id
    })
    .then((response) => { 
        console.log(response);
    })
    .catch((error) => {

    });

Above code works fine if I use it in my local system, But if I build a project and upload into hosting server proxy doesn't work.

Anyone know the solution? I have checked in github also but not found proper solution.

like image 839
JEETHESH KARKERA Avatar asked Oct 22 '25 05:10

JEETHESH KARKERA


1 Answers

I think you should define your proxy in the axios config, if it should be applied globally for every requests, you can use this code to define the Global axios default https://github.com/axios/axios#config-defaults

import axios from 'axios';

axios.defaults.proxy.host = "http://www.demoapp.com"
axios.defaults.proxy.port = ...
axios.defaults.proxy.auth.username = ...
axios.defaults.proxy.auth.password = ...
like image 94
Olivier Boissé Avatar answered Oct 24 '25 17:10

Olivier Boissé



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!