Search code examples
jqueryajaxapachefile-exists

XMLHttpRequest Exception 101: A network error occurred in synchronous requests


So I'm building a jQuery plugin to swap out normal graphics for retina ones when the user is on a high-pixel-density device. Part of that swap required checking if the retina version of the file exists at a certain url around the web. Here's how it checks:

function urlExists(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
};

But when it tries to check a file that is on a different server then the current one, it fails with this error in the console:

XMLHttpRequest Exception 101: A network error occurred in synchronous requests.

How can I allow cross-domain XML https requests so that this works on any site that loads the plugin?

Also, I'd like to eventually convert this to JS, so doing it without jQuery would be optimal.

Here's a JSFiddle: http://jsfiddle.net/JacksonGariety/7YYgP/1/ (NOTE: I've commented out the check for retina devices portion so you can test stuff)


Solution

  • Allowing cross-domain AJAX requests depends on the server configuration, not the client (as best I know). That means that there's no way to ensure a successful / allowed cross-domain AJAX request just from loading a jQuery / JS plugin alone.

    If there is, though, golly, I'd love to know about it.