跨域配置
gitcx
# 后端跨域的配置(各种后端语言不尽相同,如不是下述后端语言具体请百度查看)
nginx
location / {
if ($request_method = OPTIONS ) {
add_header Content-Type *;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'POST, GET, OPTIONS, PUT, DELETE';
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Headers *;
return 200;
}
}
php
/**
* 解决跨域访问,nginx需要在伪静态里面添加“nginx伪静态.txt”规则
*/
header('Content-Type: *');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');//设置允许访问的协议
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: *');
spring-boot
// spring-boot跨域
// 在相应的controller上加入@CrossOrigin注解
#前端代理连接方案(不推荐方案)
打开 vue.config.js 放开注释,就可以了(记住了,其他的啥都不用改,不用改动 settings.js 的 baseURL,框架会自动将 vab-mock-server 后缀代理到你的后台地址,此方式仅在开发环境临时解决跨域,建议直接后端配置,一劳永逸)
proxy: {
[baseURL]: {
target: `http://你的后端接口地址`,//所有配置不要动,只改这一个地方,改完重启项目
ws: true,
changeOrigin: true,
pathRewrite: {
["^/" + baseURL]: "",
},
},
},