最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Catching 'Blocked loading mixed active content' CORS error - Stack Overflow

programmeradmin10浏览0评论

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("/").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...

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

Share Improve this question asked Dec 12, 2013 at 21:16 Jeroen OomsJeroen Ooms 33k36 gold badges143 silver badges218 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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/ocpu/library/")

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

发布评论

评论列表(0)

  1. 暂无评论