跨域

工作中遇到的跨域问题

vue 本地起服务,端口号是 3030
vue.XXX.com:3030 代理到了 127.0.0.1(localhost)
vue.XXX.com:3030 访问后端地址 mall.XXX.com 报跨域问题 如下图所示
跨域现象
跨域现象2
携带 cookie 时,需要设置如下参数,由于安全策略的影响

1
axios.defaults.withCredentials = true; //Cookie跨域
1
2
3
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: 'http://vue.iqiyi.com:3030' // 这里不能设置*,要写死前端具体请求的域名
// 因为*太放纵太宽泛了(permissive)会破坏credentials的使用,所以必须设置允许跨域的头部的域名为具体某个

去掉浏览器同源策略影响

  • mac 环境:
    terminal 中输入以下命令,打开浏览器
1
2
3
open -n /Applications/Google\ Chrome.app/ --args --disable-web-security  --user-data-dir=/Users/XXX/Documents/MyChromeDevUserData

// XXX 就是自己mac电脑的路径 就是 @ 前面的字符
  1. 谷歌浏览器地址栏输入:chrome://flags/
  2. 找到:SameSite by default cookies、Cookies without SameSite must be secure
  3. 设置上面这两项设置成 Disable

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2024 HL's Blog All Rights Reserved.

UV : | PV :