【发布时间】:2015-08-19 05:12:15
【问题描述】:
我正在尝试检测混合内容上的 XHR 失败。看起来不同的浏览器有不同的实现:
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
}
我不想使用自动检测 (xhr.open('//otherdomain')),因为目标可能不支持 http 或 https。我只是想知道调用失败,所以我可以在我的页面中显示错误。是否可以为所有浏览器正确处理这个问题?
【问题讨论】:
-
碰巧遇到了同样的问题。如果我发现了什么,我会告诉你的。
-
@JuniorIncanter 我还没有单独解决它,但是通过在我的未来添加超时来解决这个问题,如果它没有完成则假设这是错误。
-
这也是我想出的最好的,但它仍然感觉像一个草率的创可贴。
-
还有什么比超时更优雅的吗?
-
@Philip 我还没有找到其他方法,没有。对不起。
标签: javascript xmlhttprequest mixed-content