0

I'm trying to create a wrapper for axios, so I can always use the same configuration.

The following works as expected:

function getAxiosInstance() {
  const config = {
    timeout: 3000,
    headers: {
      'Content-Type': 'application/json',
    },
  };
  return axios.create(config);
}

getAxiosInstance().get('/foo');

However, just adding baseURL: 'http://localhost:8080/api/' to the configuration (i.e. config) results in two weird requests: The first without any request method and the second one has a method of type OPTIONS.

I tried omitting the domain and port (/api/) which surprisingly worked, but that's not what I'm trying to achieve.

I'm using the latest version of axios (0.21.0) although I tried with an earlier one as well.

4
  • 1
    This is normal behaviour for browsers. It is a CORS issue Commented Nov 18, 2020 at 17:29
  • Install a CORS extension from your browser extension store Commented Nov 18, 2020 at 17:30
  • That was indeed the issue. Thanks a lot! @ManosKounelakis Commented Nov 19, 2020 at 7:23
  • 1
    Sure no problem !!! Commented Nov 19, 2020 at 8:03

1 Answer 1

1

The issue was indeed a (vaguely indicated) CORS config I had to change on the backend.

Sign up to request clarification or add additional context in comments.

Comments

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.