Define server / site support for cross-domain XMLHttpRequests?

I am interested to know what minimal cross-browser Javascript code (jQuery is fine too) may be possible to check if the site that I want to send cross-domain AJAX requests supports CORS .

I know jQuery has a way to check if the browser supports it. But I would really like to check if the remote site supports.

I thought I just needed to check the HTTP header, but of course, XHR will throw an exception when trying to do a cross-domain request for a site that doesn't support it.

But is there a way to verify that the exception thrown by XMLHttpRequest was due to the site not supporting CORS and not something else wrong? (It should not support XDomainRequest.)

In any case, I'm not sure if the best minimal / fast cross-browser way to get HTTP HEAD using AJAX, which I assume is an easy way, not a page selection, etc.

Use case. Once I find out if the browser and the AJAX provider support CORS or not, I can choose whether to send more efficient JSON / CORS requests or return to JSONP requests when necessary.

+4
source share
1 answer

As for the specification , this is not possible.

If there is a network error: in case of DNS errors, negotiating a TLS failure or other network errors, apply the network error steps. Do not request any interaction with the end user ...

Otherwise: perform a resource access check. If it returns an error, apply the network error steps ...

There is no special way to identify cases where CORS is not allowed, because network error measures are applied at any time .

+1
source

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


All Articles