How to fix Access-Control-Allow-Origin error?

I am using AngularJS, but I ran into the same issue with jQuery and in plain JavaScript. If the remote server I'm accessing sets Access-Control-Allow-Origin, then the success or error method is called.

If Access-Control-Allow-Origin does not exist, none of them are called, and I can only see the error in the console. Is there a way to capture the No Access-Control-Allow-Origin error in my JavaScript code?

And just in case, if you are interested in a use case, I create an interface for PyPi https://pypi.python.org/ There you can get information about packages in JSON format. (e.g. https://pypi.python.org/pypi/dspy/json ). These requests set Access-Control-Allow-Origin, but if I send a request for a package that is not on the server (for example, https://pypi.python.org/pypi/dspyz/json ), then I get No Access-Control -Allow-Origin, and therefore I do not get anything that would be called in my JavaScript code.

Update

After several experiments, I found out that the error method was called, it just threw an exception due to some problem with the code that I used and which was misleading to me.

+6
source share
1 answer

This is not possible for security reasons. Detecting a local network of users would be possible.

More info here .

Link credits to eithedog for your comment.

+3
source

Source: https://habr.com/ru/post/985368/


All Articles