Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

方法一

定义一个配置类,实现WebMvcConfigurer接口,重写addCorsMappings方法

1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
public class CorsConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowCredentials(true)
.allowedMethods("*")
.maxAge(3600);
}
}

方法二

在xml中添加如下配置:

1
2
3
<mvc:cors>
<mvc:mapping path="/**"/>
</mvc:cors>

方法三

在vue项目中
vue.config.js内配置代理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module.exports = defineConfig({
transpileDependencies: true,
devServer: {
// 配置代理
proxy:{
'/api':{ // 所有以/api开头的请求都会被代理到target上
target: 'http://localhost:8080', // 代理的目标地址
changeOrigin: true, // 支持跨域
pathRewrite: { // 重写路径
'^/api': ''
}
}
}
}

在main.js中配置axios:

1
2
3
4
// axios配置
import axios from 'axios'
axios.defaults.baseURL = '/api/'
Vue.prototype.$http = axios

在组件中method中定义方法:

1
2
3
async getCode(){
const {data:res} = await this.$http.get('captchaImage')
},

评论