vue+axios的跨域问题

是一个前后端分离的项目,前端使用的是vue+axios

const service = axios.create({
  // 为防止 no-referrer-when-downgrade 先使用http
  baseURL: 'API地址',
  timeout: 5000,
  headers: {
    'content-type': 'application/json;charset=UTF-8',
    '某token': '这里是token'
  }
})
  • 当前端访问后端取数据的时候,OPTIONS 返回的是307:

图片描述

  • 浏览器调试信息下面报错:
Access to XMLHttpRequest at 'http://API地址?参数=xxx' from origin 'http://前端地址:端口' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

这种情况是如何引起的,该怎么处理呢?

阅读 5.5k
2 个回答
proxyTable: {
      '/apis': {
        target: 'http://127.0.0.1:8099'  // 接口域名
        changeOrigin: true,  //是否跨域
        pathRewrite: {
            '^/apis': ''   //需要rewrite重写的,
        }              
      }

this.$axios.post('/apis/test/testToken.php',data).then(res=>{
        console.log(res)
})

这样用了吗

vue-cli为我们提供了代理方案,在config文件夹下的index.js中添加以下代码,
代码

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题