springboot端
@Configuration
public class CorsConfig implements WebMvcConfigurer {
static final String ORIGINS[] = new String[] { "GET", "POST", "PUT", "DELETE" };
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS)
.maxAge(3600);
}
}
vue端可以用的方法有三种:https://www.cnblogs.com/wasbg/p/10973880.html[vue跨域]
(https://www.cnblogs.com/wasbg/p/10973880.html)
自己用的是这个vue脚手架跨域问题-代理服务器proxy配置 - 凉城i - 博客园 https://www.cnblogs.com/WorldEye/p/13605451.html
module.exports = {
devServer: {
host: 'localhost',
port: 8080,
proxy: {
'/api': {
target: 'http://localhost:3000',// 要跨域的域名
changeOrigin: true, // 是否开启跨域
},
'/get': {
target: 'http://localhost:3000',// 要跨域的域名
changeOrigin: true, // 是否开启跨域
}
}
}
}
vue.config.js有个问题是使用这种方法时最好重启一下serve