“重定向已被 CORS 政策阻止"Vue Axios [英] "Redirect has been blocked by CORS policy" Vue Axios

查看:133
本文介绍了“重定向已被 CORS 政策阻止"Vue Axios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要将此标记为重复.我已阅读有关堆栈溢出的所有相关答案,但尚未针对此问题找到明确的解决方案.

Please don't mark this as a duplicate. I have read every related answer on stack overflow and have yet to come to a clear solution for this issue.

我现在正在使用带有 axios 的 vue 服务器来尝试从 https://coinmarketcap.com 获取数据并显示它.问题是我在 chrome 控制台中收到一条错误消息:

I'm using a vue server right now with axios to try to fetch data and display it from https://coinmarketcap.com. The problem is that I get an error message in the chrome console:

从'https://api.coinmarketcap.com/v1/ticker' 重定向到'https://api.coinmarketcap.com/v1/ticker/' 已被屏蔽通过 CORS策略:没有Access-Control-Allow-Origin"标头存在于请求的资源.Origin 'http://mywebsite.ca' 因此不是允许访问.

Redirect from 'https://api.coinmarketcap.com/v1/ticker' to 'https://api.coinmarketcap.com/v1/ticker/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mywebsite.ca' is therefore not allowed access.

我的函数尽可能简单,符合文档和在线教程.在我的 vue 组件脚本部分:

My function is as barebones as possible, in line with documentation and online tutorial. In my vue component script section:

methods: {
      async getCryptos() {
        await axios.get('https://api.coinmarketcap.com/v1/ticker').then((resp) => {
          this.response = resp.data
        }).catch((err) => {
          this.response = err
        })
      }
    },
    beforeMount () {
      this.getCryptos()
    }

我已经阅读了无数关于这个主题的文档,但还没有找到明确的答案.我不知道要包含哪些标头,但我尝试了多种配置组合.有人可以简洁地解释一下我需要在这里做什么吗?我知道其他网站从 coinmarketcap 获取,所以这是可能的.应该没那么难...

I've read countless documentation on this subject and have yet to find a clear answer. I have no idea what headers to include but I have tried many combinations of configuration. Can someone succinctly explain what it is that I need to do here? I know other websites fetch from coinmarketcap, so it is possible. It shouldn't be that difficult...

推荐答案

我通过在 API 请求末尾添加斜杠 (/) 解决了我的问题.我在控制台日志中收到了同样的警告,但现在可以正常工作了.

I fixed my issue by adding the slash (/) at the end of my API request. I've been getting the same warning at my console log but it's working now.

这篇关于“重定向已被 CORS 政策阻止"Vue Axios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆