开发者社区> 问答> 正文

浏览器跨域?报错

@王念博客 你好,想跟你请教个问题:我xml配置

<mvc:cors>
	<mvc:mapping path="/**" />
</mvc:cors>

但是还是报错:XMLHttpRequest cannot load http://127.0.0.1:8080/daily-life/account/signin. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

展开
收起
爱吃鱼的程序员 2020-06-08 17:25:01 484 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    在过滤器中设置下响应头就可以了,具体百度吧

    过滤器中加这段代码

    @OverridepublicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{//设置允许跨域HttpServletResponseresponse=(HttpServletResponse)servletResponse;response.setHeader("Access-Control-Allow-Origin","*");response.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,PUT,DELETE");response.setHeader("Access-Control-Allow-Credentials","true");response.setHeader("Access-Control-Allow-Headers","*,token,activityId,activityType");response.setHeader("Access-Control-Expose-Headers","*");filterChain.doFilter(servletRequest,servletResponse);}

    <mvc:cors>
        <mvc:mappingpath="/**"allowed-origins="*"allow-credentials="true"max-age="1800"allowed-methods="GET,POST,OPTIONS"/>
      </mvc:cors>

    我是这么加的是可行的

    直接升级Spring版本,有注解可以使用

    @CrossOrigin
    2020-06-08 17:25:17
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
WEB浏览器中即将发生的安全变化 立即下载
基于浏览器的实时构建探索之路 立即下载
基于浏览器的实时构建探索之路--玄寂 立即下载