Javascript – Catching ‘Blocked loading mixed active content’ CORS error

ajaxcorsfirefoxjavascriptjquery

In firefox, when javascript attempts to do a CORS request to a http server from a page that is hosted on https, it will throw an error:

Blocked loading mixed active content 

I would like to catch these errors, but I can't figure out how. E.g.I tried something like this with jQuery:

try {
  $.get("http://public.opencpu.org/ocpu/library/").fail(function(xhr, err){
    console.log("Server error:" + xhr.responseText);
  });
} catch(e){
  console.log(e.message);;
}

But buth xhr.responseText and e.message are empty strings (probably because $.ajax happens asynchronously). How can I catch the actual error message that says: Blocked loading mixed active content…

Best Answer

You cannot catch that exact message. It will only be logged to the console, but is not available to the user.

When using plain XHR, the .open() call would throw an exception that has .result === 0x805e0006. Also, ex.toString() will contain nsresult: "0x805e0006 (<unknown>)".

jQuery, puts ex.toString() into the .statusText of the jqXHR, so you could do the following check for requests blocked due to mixed content:

xhr.statusText.indexOf('nsresult: "0x805e0006 (<unknown>)"') > -1

For the curious: 0x805e0006 should be NS_ERROR_CONTENT_BLOCKED (C++ macro value).