为什么在 crossOrigin = 'Anonymous' 图像上设置 base64 数据时,Safa

2023-09-30前端开发问题
85

本文介绍了为什么在 crossOrigin = 'Anonymous' 图像上设置 base64 数据时,Safari 会引发 CORS 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到一个问题,将图像 src(使用 new Image 创建的图像)设置为 base64 编码图像失败,原因是:跨域资源共享策略拒绝跨域图像加载.

I'm having an issue where setting an image src (image created with new Image) to a base64 encoded image fails by throwing: Cross-origin image load denied by Cross-Origin Resource Sharing policy.

我已经有 image.crossOrigin = 'Anonymous'了.

请参阅以下代码笔:http://codepen.io/bedeoverend/pen/aORQzg.它适用于 Chrome,但不适用于 Safari.如果可以,黑白饼图图像应该会显示在底部.

See the following codepen: http://codepen.io/bedeoverend/pen/aORQzg. It works in Chrome, but not in Safari. The black and white pie chart image should show up down the bottom if it worked.

为什么会发生这种情况以及如何解决?

Why does this happen and how can it be resolved?

更新:

为了澄清,我在这里做了一个更集中的代码笔:http://codepen.io/bedeoverend/pen/BNGarr

To clarify, I've made a more focused codepen here: http://codepen.io/bedeoverend/pen/BNGarr

似乎在图像上设置 crossOrigin = 'Anonymous' 然后在 Safari 中加载 base64 失败.在 Chrome 44 上,这两个文本都更改为 Worked...,但在 Safari 8.0.3 上,跨源匿名一个失败.在 Firefox 35 & 上也失败了39.

It seems that setting crossOrigin = 'Anonymous' on an image then loading in base64 fails for Safari. Both text changes to Worked... on Chrome 44, but on Safari 8.0.3, the cross origin Anonymous one fails. Also fails on Firefox 35 & 39.

所以现在的问题本质上是,为什么当图像的 crossOrigin 属性设置为匿名"时,Safari 无法加载 base64 图像?

So the question is essentially now, why, when an Image's crossOrigin property is set to 'Anonymous', does Safari fail to load base64 images in?

推荐答案

MDN 声明您必须有一个服务器托管具有适当的 Access-Control-Allow-Origin 标头的图像."

MDN states that "You must have a server hosting images with the appropriate Access-Control-Allow-Origin header."

根据到规格,
强调我的

不幸的是,这可用于对用户的本地网络执行基本的端口扫描[...]用户代理可以实施更严格<的跨域访问控制策略/strong> 比上面描述的更能减轻这种攻击,但不幸的是,这些策略通常与现有的 Web 内容不兼容."

This, unfortunately, can be used to perform a rudimentary port scan of the user's local network [...] User agents may implement cross-origin access control policies that are stricter than those described above to mitigate this attack, but unfortunately such policies are typically not compatible with existing Web content."

您可以通过检查 url 字符串来简单地解决它,如果它以 data: 开头,那么它可能不会与跨源标头一起提供,那么您可以将 crossOrigin 属性设置回 null.

You could simply workaround it by checking the url string, if it starts with data: then it probably won't be served with the cross origin header, then you can set the crossOrigin property back to null.

这篇关于为什么在 crossOrigin = 'Anonymous' 图像上设置 base64 数据时,Safari 会引发 CORS 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13