<>一、问题再现
<>二、分析问题
项目中采用Vue+SpringBoot技术搭建前后端分离系统,出现问题的原因是前后端分离系统中存在跨域问题。
<>三、解决问题
<>1、控制器中已经添加@CrossOrigin注解,但是仍然有问题。
<>2、经过网上查阅资料,出现此问题的原因是我在springboot中设置了拦截器。解决方法如下:
public class JWTInterceptor implements HandlerInterceptor { @Override public
boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception { String origin = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", origin); response.setHeader(
"Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.
setHeader("Access-Control-Max-Age", "3600"); //
response.setHeader("Access-Control-Allow-Headers",
"x-requested-with,Authorization"); response.setHeader(
"Access-Control-Allow-Headers", "Content-Type,Access-Token"); //
解决vue-axios请求报错问题 response.setHeader("Access-Control-Allow-Credentials", "true")
; }