4

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.

1
  • "doesn't work" is virtually meaningless. Inspect actual requests in browser dev tools network for clues Commented Sep 6, 2018 at 13:16

1 Answer 1

6

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 = ...
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for the answer. It worked :) But instead of "proxy.host" it mentioned in "axios.defaults.proxy.host" I have used "baseURL ("axios.defaults.baseURL")" as it mentioned in GITHUB link which you have provided :)
I am looking for global proxy solution which works on all of my files,not on only one as above.
From the documentation github.com/axios/axios#config-defaults : You can specify config defaults that will be applied to every request. so this configuration will be applied globally to every requests

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.