Catching 'Blocked loading mixed active content' CORS error

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. EgI 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...


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).


You should be able to prevent the error in the first place by using a protocol relative URL:

$.get("//public.opencpu.org/ocpu/library/")

uses either http or https, depending on your page context. public.opencpu.org supports both, so no problem there.

链接地址: http://www.djcxy.com/p/17920.html

上一篇: 使用Qt Creator调试Qt应用程序:<无此值>

下一篇: 抓住'阻塞加载混合活动内容'CORS错误