最近在做一个vue前后端分离的项目,但是每次ajax请求时session都会变化,搜索资料后的处理办法总结如下:

    1:ajax请示请加入

            xhrFields: {
                  withCredentials: true
            }

   或使用axios(待验证);

    2:在java后台中加入拦截器,Access-Control-Allow-Credentials 设为true

        @Override
	public void intercept(ActionInvocation ai) {
		// TODO Auto-generated method stub
		Controller c = ai.getController();
		c.getResponse().setHeader("Access-Control-Allow-Credentials", "true");  
		c.getResponse().setHeader("Access-Control-Allow-Origin", c.getRequest().getHeader("Origin"));  
		c.getResponse().setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");  
		ai.invoke();
	}
以上两步可以解决浏览器跨域的问题
Logo

前往低代码交流专区

更多推荐