I am interested in seeing what minimal cross-browser Javascript code (jQuery is fine too) might be possible for verifying whether a site I wish to send cross-domain AJAX requests has support for CORS
.
I know jQuery has a method to check whether the browser supports it. But I'd really like to be able to check if the remote site also supports it.
I thought I just had to check for a HTTP header but of course the XHR will throw an exception when trying to do a cross-domain request to a site which doesn't specifically support it.
But is there a way to check that an exception thrown by XMLHttpRequest
was due to the site not supporting CORS
rather than something else going wrong? (It doesn't have to support XDomainRequest.)
In any case I'm not sure the best minimal/fastest cross-browser way to to fetch an HTTP HEAD
using AJAX, which I assume is the lightweight way rather than fetching a page, etc.
Use case: Once I know whether the browser and AJAX provider support CORS
or not, I can choose whether to send the more featureful JSON
/CORS
requests or fall back to JSONP
requests when necessary.