简体   繁体   English

fabric.Image.fromURL上的交叉原点即使使用crossOrigin设置也会中断CORS

[英]Cross origin on fabric.Image.fromURL breaks CORS even with crossOrigin set

Using fabric.js I'm attempting to load a background image from an amazon cloudfront/s3 url. 我正在尝试使用fabric.js从Amazon cloudfront / s3 url加载背景图像。

fabric.Image.fromURL(imgURL, function(img) {
    canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas),
    {
        scaleX: canvas.width / img.width,
        scaleY: canvas.height / img.height
    });
}, {crossOrigin: 'anonymous'});

When crossOrigin is present, I get the standard error 当存在crossOrigin时,出现标准错误

Access to image at '...' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

When I remove the crossOrigin anon. 当我删除crossOrigin anon时。 It will load the image, however it taints the canvas so that I cannot use toDataURL . 它将加载图像,但是会弄脏画布,因此我不能使用toDataURL

CORS标头

When I remove the crossOrigin anon. 当我删除crossOrigin anon时。 It will load the image, however it taints the canvas so that I cannot use toDataURL. 它将加载图像,但是会弄脏画布,因此我不能使用toDataURL。

This is the default position. 这是默认位置。 If you don't specify it, then it won't allow you to do anything that requires CORS permission. 如果未指定,那么它将不允许您执行任何需要CORS权限的操作。

When crossOrigin is present, I get the standard error 当存在crossOrigin时,出现标准错误

Access to image at '...' from origin ' http://localhost:3000 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 从源' http:// localhost:3000 '对'...'处的图像的访问已被CORS策略阻止:请求的资源上没有'Access-Control-Allow-Origin'标头。

anonymous means "Check for CORS permission, but don't send user credentials to a different origin". anonymous表示“检查CORS权限,但不要将用户凭据发送到其他来源”。

So instead of just forbidding access to features that need CORS permission, it first checks for CORS permission, discovers that you don't have it, then forbids access. 因此,它不仅禁止访问需要CORS权限的功能,还首先检查CORS权限,发现您没有此权限, 然后禁止访问。

You need the server you are requesting the image from to provide your JavaScript with permission to read the data using CORS. 您需要从中请求图像的服务器,以向您的JavaScript授予使用CORS读取数据的权限。

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

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