Error: Access-Control-Allow-Origin, for ajax request

Asked

Viewed 568 times

-1

For the AJAX request below:

var url = "minhaurlaqui";
$.ajax({url: url,  success: function (retorno)
}

I have the following return:

"Access to Xmlhttprequest at 'urlaqui' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested Resource."

I can not adjust this anywhere, someone could tell me what is missing to do in the Ajax request ?

  • Hello Diogo! Can you please tell me which URL you are trying to request? It would be a local URL?

  • Fastest way: Using an extension. I’ve never done this but I found this one. https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi If you are using any logic on your endpoint, speak here, as it is possible to enable this for you.

  • Yes, but below I received the answer to doubt, who blocks is browser, anyway thank you.

1 answer

2


Nothing is missing from your request (although the successful callback is not set).

The browser that is blocking the answer because of the cross-origin policy. Since you are making a request for a non-local route, your browser assumes that you may be trying to steal resources from another website.

In the answer to the service you are trying to use, there needs to be a header Access-Control-Allow-Origin: [seu website] or Access-Control-Allow-Origin: * so that the browser understands that this server response can actually be delivered to the user.

Note that the browser does not prevent you from sending the request, nor does the server respond with an error, the server actually processed your request and gave you an answer, but the browser refused to display it.

  • Understood, thank you very much, it was well this answer that needed.

Browser other questions tagged

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