捕获CORS错误 [英] Catch CORS error

查看:254
本文介绍了捕获CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试载入CORS已停用的图片,并收到错误讯息:

I'm trying to load a CORS-disabled image, and get the error:

Cross-origin image load denied by Cross-Origin Resource Sharing policy.

我尝试捕获错误如下,但显然不会工作。

I've tried catching the error as follows, but that obviously will not work.

如何在设置<$ c $后捕获CORS错误c> .src 图片的属性?

How can I catch CORS errors after setting the .src property of an image?

推荐答案

使用 onError 事件

if(image.addEventListener) {
    image.addEventListener('error', function (e) {
        e.preventDefault(); // Prevent error from getting thrown
        // Handle error here
    });
} else {
    // Old IE uses .attachEvent instead
    image.attachEvent('onerror', function (e) {
        // Handle error here
        return false; // Prevent propagation
    });
}

代码应该合并,这样你就不必两次写代码,但希望你能有这个想法。

Code should probably be consolidated so you don't have to write your code twice, but hopefully you've got the idea.

这篇关于捕获CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆