ios ajax cookies,Vue 使用axios获取cookies

axios获取后端代码传来的cookie

vue配置中加入

axios.defaults.withCredentials = true; //让ajax携带cookie

但是在请求访问的时候又会报错

446f38217910b29cc20fa6776df45218.png

所以后端设置一个过滤器

@Componentpublic class CorsFilter implementsFilter {

@Overridepublic void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throwsIOException, ServletException {

HttpServletResponse response=(HttpServletResponse) res;

HttpServletRequest reqs=(HttpServletRequest) req;//response.setHeader("Access-Control-Allow-Origin",reqs.getHeader("Origin"));

response.setHeader("Access-Control-Allow-Origin","http://localhost:8080");//****

response.setHeader("Access-Control-Allow-Credentials", "true");

response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");

response.setHeader("Access-Control-Max-Age", "3600");

response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

chain.doFilter(req, res);

}

@Overridepublic voidinit(FilterConfig filterConfig) {}

@Overridepublic voiddestroy() {}

}

(搞了很久才行,本来用的是*,但是又报错The value of the ‘Access-Control-Allow-Origin‘ header in the response must not be the wildcard ‘*‘ when the request‘s credentials mode is ‘include‘. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.)

说是跨域时*引起的,所以,就把它改成了http://localhost:8080

原文:https://www.cnblogs.com/xymaxbf/p/12535276.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值