简体   繁体   English

仅在 Windows 上被 CORS 阻止访问图像

[英]Access to image has been blocked by CORS only on Windows

Vue component has a photo block and the "edit" button. Vue 组件有一个照片块和“编辑”按钮。

<template>
    <div>
        <tui-image-editor ref="editor" > </tui-image-editor>
        <div class="">
            <img :src="img">
            <button @click="edit()">Edit</button>
        </div>
    </div>
</template>
<script>
export default {
  data() {
    return {
      img: "cdn.domain.shop/eaa49b02e350627622904290a83599d6.png",
    };
  },
  methods: {
    edit() {
      this.$refs.editor.invoke("loadImageFromURL", this.img, "Editable image");
    },
  },
};
</script>

As a photo editor, I use TUI image editor .作为照片编辑器,我使用TUI 图像编辑器 In the click handler, I pass the url to the editor by loadImageFromURL function在点击处理程序中,我通过loadImageFromURL函数将 url 传递给编辑器

when I click the "edit" button in Chrome in Windows I get an error当我在 Windows 的 Chrome 中单击“编辑”按钮时出现错误

Access to image at 'cdn.domain.shop/eaa49b02e350627622904290a83599d6.png' from origin 'example.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. CORS 策略已阻止从源“example.org”访问“cdn.domain.shop/eaa49b02e350627622904290a83599d6.png”中的图像:请求的资源上不存在“Access-Control-Allow-Origin”标头。

But when I do the same thing in Chrome in Ubuntu, everything works fine.但是当我在 Ubuntu 的 Chrome 中做同样的事情时,一切正常。

What am I doing wrong?我究竟做错了什么?

just add random string to url只需将随机字符串添加到 url

this.$refs.editor.invoke("loadImageFromURL",this.img+'?'+Math.random(), "Editable image");

the error was due to caching in the browser该错误是由于浏览器中的缓存造成的

After that you have to make sure that every URL you request from Chrome and Safari uses http:// instead of https:// .之后,您必须确保从 Chrome 和 Safari 请求的每个 URL 都使用http://而不是https:// HTTPS retrieval will not work in these browsers at all. HTTPS 检索在这些浏览器中根本不起作用。

some allows both http and https requests I solved it with a small regular expression that replaced our https URL string with http.有些允许http和https请求我用一个小的正则表达式解决了它,用http替换了我们的https URL字符串。

What's the quick solution ?什么是快速解决方案?

Add the attribute crossorigin="anonymous" in the <img> tag that displays the image before opening it in the editor.在编辑器中打开图像之前,在显示图像的<img>标签中添加属性crossorigin="anonymous" ie: <img src="targetUri" crossorigin="anonymous" />即: <img src="targetUri" crossorigin="anonymous" />

Explain the issue and solution说明问题和解决方案

The main issue is related to caching and how the browser send the Origin header.主要问题与缓存以及浏览器如何发送Origin标头有关。

First you have to know that by default the browser does not send the Origin header when you load an image with the <img> tag that does not have the crossorigin="anonymous" attribute.首先,您必须知道,默认情况下,当您加载带有<img>标签且没有crossorigin="anonymous"属性的图像时,浏览器不会发送Origin标头。 More info 更多信息

What's happening is that the browser tries to load the image from the <img> tag before the image editor is opened, and the puts it into its cache.发生的事情是浏览器在打开图像编辑器之前尝试从<img>标签加载图像,并将其放入缓存中。

So when you open the editor, it tries to load the image a second time, and you actually get a cached response of the first request that was made without the Origin header.因此,当您打开编辑器时,它会尝试第二次加载图像,并且您实际上会获得第一个没有Origin标头的请求的缓存响应。 Without this header, that cached response does not contain all the allow-control-* headers necessary to pass the CORS check, that why you get the error.如果没有此标头,该缓存的响应将不包含通过 CORS 检查所需的所有allow-control-*标头,这就是您收到错误的原因。

You can check this, by opening Chrome's inspector with "disable cache" checked.您可以通过选中“禁用缓存”打开 Chrome 的检查器来检查这一点。 It should work.它应该工作。 The previous posts that suggested to include a parameter ?t=<random_number> had the effect to bypass the browser cache, but this solution is not possible when using pre-signed urls.之前建议包含参数?t=<random_number>帖子具有绕过浏览器缓存的效果,但是在使用预签名的 url 时无法使用此解决方案。

So adding crossorigin="anonymous" in the img tag should solve the problem.所以在 img 标签中添加crossorigin="anonymous"应该可以解决问题。

暂无
暂无

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

相关问题 访问已被 CORS 问题阻止 - Access has been blocked by CORS issue 如何修复“对 XMLHttpRequest 的访问已被 CORS 策略阻止”重定向不允许仅用于一条预检请求 - how to fix 'Access to XMLHttpRequest has been blocked by CORS policy' Redirect is not allowed for a preflight request only one route CORS 政策已阻止从源 [域] 访问 [Img 链接] 中的图像 - Access to image at [Img Link] from origin [domain] has been blocked by CORS policy 从源头开始对XMLHttpRequest的访问已被CORS策略阻止 - Access to XMLHttpRequest at from origin has been blocked by CORS policy CORS 从源头访问 X 处的 XMLHttpRequest 已被 CORS 策略阻止 - CORS Access to XMLHttpRequest at X from origin has been blocked by CORS policy Axios 请求已被 cors 阻止 没有'Access-Control-Allow-Origin' header 存在于请求的资源上 - Axios request has been blocked by cors no 'Access-Control-Allow-Origin' header is present on the requested resource 如何解决“Redirect has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header'? - How to solve 'Redirect has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header'? 如何修复nuxtjs中的CORS策略阻止从源头对XMLHttpRequest的访问? - How to fix Access to XMLHttpRequest at from origin has been blocked by CORS policy in nuxtjs? CORS 策略已阻止从源“http://localhost:3000”访问“https://***”处的 XMLHttpRequest - Access to XMLHttpRequest at 'https://***' from origin 'http://localhost:3000' has been blocked by CORS policy CORS 策略已阻止从源“http://localhost:8000”访问“https://URL”处的 XMLHttpRequest: - Access to XMLHttpRequest at 'https://URL' from origin 'http://localhost:8000' has been blocked by CORS policy:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM