A
A
Andrey Okhotnikov2019-05-30 12:46:37
CORS
Andrey Okhotnikov, 2019-05-30 12:46:37

How to send an Axios cross-domain request?

Sending a request

const config = {
  withCredentials: true,
};

const get = (url, options = {}) => axios.get(`${API_URL}/${url}`, { ...options, ...config });;


I get an error
Access to XMLHttpRequest at '<API domain>' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.


How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-05-30
@tsepen

You need to add headers on the server:

'Access-Control-Allow-Origin': '*' // * или ваш домен
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE'
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'

If you are not going to set cookies from the server, then you do not need to pass the withCredentials property.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question