账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    关于 vue-qr 这个插件跨域问题
    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
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 离人未归 普通会员 1楼

      Vue-QR 是一个用于在 Vue.js 应用程序中生成二维码的库。在跨域问题方面,Vue-QR 通常需要解决以下几种情况:

      1. CORS(跨域资源共享):Vue-QR 提供了 fetch API,用于异步请求数据。在 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 => { // 处理错误 });

      1. Response的 Content-Type 设置:在 fetch 请求时,你可以指定 Content-Type 指定,告诉浏览器发送何种类型的响应。默认情况下,fetch 会发送 application/json 格式的数据。如果你的服务器返回的是一个 text/plaintext/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 => { // 处理错误 });

      1. 使用 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 => { // 处理错误 }); ```

      1. 使用 vue-qronRequestonResponse 方法vue-qr 提供了 onRequestonResponse 方法,用于在数据请求和响应过程中处理并发请求。在这些方法中,你可以设置 crossOrigin 参数,用于设置跨域请求的允许域名或 *,并设置 withCredentials 参数,用于设置 Access-Control-Allow-Origin

      以下是一个使用 vue-qronRequestonResponse 方法的示例:

      ```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 库中处理跨域问题的常见方法,具体使用哪种方法取决于你的需求和服务器的配置。注意,跨域请求可能会对性能和响应速度产生影响,因此在生产环境中,你可能需要根据具体情况进行权衡。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部