- 18
- 0
报错:
Access to Image at 'http://xxx.yyyyyyyy.com/app/p...' from origin 'http://localhost:9527' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9527' is therefore not allowed access.
但是,
如果直接使用img标签可以展示出图片
<img src="http://xxx.yyyyyyyy.com/app/p...;>
请问大家碰到过吗,有什么好的方案???
- 共 0 条
- 全部回答
-
离人未归 普通会员 1楼
Vue-QR 是一个用于在 Vue.js 应用程序中生成二维码的库。在跨域问题方面,Vue-QR 通常需要解决以下几种情况:
- CORS(跨域资源共享):Vue-QR 提供了
fetchAPI,用于异步请求数据。在fetch中,你可以使用withCredentials参数来设置Access-Control-Allow-Origin。这个参数的值应该是你要请求的服务器的域名或*,这将允许跨域请求。
javascript fetch('https://example.com/data', { method: 'GET', headers: { 'Content-Type': 'application/json' }, withCredentials: true }) .then(response => response.json()) .then(data => { // 使用数据 }) .catch(error => { // 处理错误 });- Response的 Content-Type 设置:在
fetch请求时,你可以指定Content-Type指定,告诉浏览器发送何种类型的响应。默认情况下,fetch会发送application/json格式的数据。如果你的服务器返回的是一个text/plain或text/html格式的数据,你需要指定Content-Type为相应的值,例如:
javascript fetch('https://example.com/data', { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'text/plain' }, withCredentials: true }) .then(response => response.text()) .then(data => { // 使用数据 }) .catch(error => { // 处理错误 });- 使用
fetch的 proxy(代理):如果你的服务器需要对跨域请求进行身份验证或者返回特定的响应,可以使用proxy参数设置代理服务器。以下是一个使用proxy设置代理的示例:
```javascript const url = 'https://example.com/data'; const proxy = new Proxy(url, { get: function (target, key) { if (key === 'data') { return data; } return target[key]; } });
fetch(proxy, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'text/plain' }, withCredentials: true }) .then(response => response.text()) .then(data => { // 使用数据 }) .catch(error => { // 处理错误 }); ```
- 使用
vue-qr的onRequest和onResponse方法:vue-qr提供了onRequest和onResponse方法,用于在数据请求和响应过程中处理并发请求。在这些方法中,你可以设置crossOrigin参数,用于设置跨域请求的允许域名或*,并设置withCredentials参数,用于设置Access-Control-Allow-Origin。
以下是一个使用
vue-qr的onRequest和onResponse方法的示例:```javascript import VueQR from 'vue-qr';
VueQR.config({ crossOrigin: 'https://example.com/data', withCredentials: true });
export default new VueQR({ onRequest: (config, request) => { const origin = config.origin || '*'; const data = request.data;
if (data && data.crossOrigin) { origin = data.crossOrigin; } if (origin === '*') { // 处理所有的跨域请求 console.log(`Request from ${origin}`); } else if (origin === '*') { // 处理只有请求来自 `https://example.com/data` 的跨域请求 console.log(`Request from ${origin} as it is from ${config.origin}`); } else { // 处理请求来自指定域名或 `*` 的跨域请求 console.log(`Request from ${origin} as it is from ${config.origin}`); }}, onResponse: (response, data) => { if (data && data.crossOrigin) { data.crossOrigin = 'https://example.com/data'; }
if (data && data.crossOrigin === 'https://example.com/data') { // 处理所有的响应 console.log(`Response from ${data.crossOrigin}`); } else if (data && data.crossOrigin === 'https://example.com/data') { // 处理只有响应来自 `https://example.com/data` 的响应 console.log(`Response from ${data.crossOrigin} as it is from ${response.origin}`); } else { // 处理请求来自指定域名或 `*` 的响应 console.log(`Response from ${data.crossOrigin} as it is from ${response.origin}`); }} }); ```
以上是在 Vue-QR 库中处理跨域问题的常见方法,具体使用哪种方法取决于你的需求和服务器的配置。注意,跨域请求可能会对性能和响应速度产生影响,因此在生产环境中,你可能需要根据具体情况进行权衡。
- CORS(跨域资源共享):Vue-QR 提供了
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
