有兩種方式,一種是在前端配置,一種是在后端配置
在前端配置代理
devServer: {
port: 7070,
proxy: {
'/api': {
target: 'http://localhost:8080',//如果不行可以寫'^/api':''
changeOrigin: true,
},
},
}
在后端設(shè)置跨域資源共享(CORS)的注解
@CrossOrigin("http://localhost:7070")
public class TestController {
@GetMapping("/test")
public R<String> test(){
String s = "帥哥";
return R.success(s);
}
}
跨域攜帶cookie等憑證的問題
需要在前后端都配置
在前端新建axios的時(shí)候添加withCredentials: true文章來源:http://www.zghlxwxcb.cn/news/detail-477827.html
const _axions = axios.create({
baseURL: 'http://localhost:8080',
withCredentials: true
});
在后端添加文章來源地址http://www.zghlxwxcb.cn/news/detail-477827.html
@CrossOrigin("http://localhost:7070",allowCredentials = "true")
到了這里,關(guān)于開發(fā)環(huán)境中解決跨域問題,nginx和tomcat的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!