@王念博客 你好,想跟你请教个问题:我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.
在过滤器中设置下响应头就可以了,具体百度吧
过滤器中加这段代码
@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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。