方式1:
vue项目中vue.config.js中增加代理devserver:其中proxy内容为:
proxy: {
"/api": {
target: "http://localhost:8002", //接口地址
secure: false, //表示接受对 https 的代理,这在目标服务器使用自签名证书时很有用。
changeOrigin: true, //用于控制 Host 头的值。如果为 true,Host 头会被修改为目标 URL 的主机名。
pathRewrite: {
"/api": "", //将路径中的 /api 替换为空字符串,这意味着例如 /api/user 会被转发为 http://localhost:8002/user。
},
},
}
方式2:
在后台接口增加允许跨域访问的配置:
@Override
public void addCorsMappings(CorsRegistry registry) {
//所有请求都允许跨域
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*");
}