I'm trying to detect an XHR failing on mixed content. 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. 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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.