Access-Control-Allow-Origin error

Asked

Viewed 1,105 times

0

Personal I am trying to make a request from one server to another to perform a registration, but I always get returned this error:

Failed to load http://domain: Response to preflight request doesn’t pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://dominio_que_solicita.com.br' is therefore not allowed access.

My requisition is like this:

var config = {
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': '*',
        'token': 'meu_token'
    }
};
this.$http.post('http://dominio_da_api/services/criarConta', this.dados, config).then(response => {

I used a Chrome extension for CORS tests when it is enabled works normally, but when I disable it generates this error. I am using Codeigniter and for the requests Vue-Resource, tbm I used Axios, but it returns me the same error

1 answer

1

I had a problem a while ago with this trying to make requests in the Google Maps API, and this error occurred on the front end (Vuejs2), but through Postman worked.

I didn’t understand the problem, but I solved this by first making a request in my own back-end API that I had in the project in Laravel, and Laravel made the request for the Google Maps API, and then returned to my front-end.

In this way, my front-end always remains accessing my back-end, and the back-end becomes responsible for accessing third-party API(s).

I hope it helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.