简体   繁体   English

捕获混合内容错误

[英]Catching Mixed content error

I'm trying to detect an XHR failing on mixed content.我正在尝试检测 XHR 在混合内容上失败。 It looks like different browsers have different implementations:看起来不同的浏览器有不同的实现:

var xhr = new XMLHttpRequest();
try {
  xhr.open('http://otherdomain/');
} catch (err) {
  console.log(err); // IE10 hits this one
}
try {
  xhr.send(); // Chrome fails here, but doesn't throw an error
} catch (err) {
  console.log(err); // No browser I've tried hits this one
}

I don't want to use autodetection ( xhr.open('//otherdomain') ), since the target might not support http, or https.我不想使用自动检测( xhr.open('//otherdomain') ),因为目标可能不支持 http 或 https。 I simply want to know the call failed, so I can show an error in my page.我只是想知道调用失败,所以我可以在我的页面中显示错误。 Is it possible to handle this correctly for all browsers?是否可以为所有浏览器正确处理这个问题?

There is no way to handle this error with javascript unfortunately.不幸的是,没有办法用 javascript 处理这个错误。 It is a security restriction enforced by the browser and thrown on a lower level.它是由浏览器强制执行并在较低级别上执行的安全限制。 I have tried so many different methods, including the use of extensions but nothing worked except a timeout handler to be honest.我尝试了很多不同的方法,包括使用扩展,但老实说,除了超时处理程序外,没有任何效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM