浏览器跨域

chenyouhong 发布于 2017/02/05 22:42
阅读 269
收藏 0

@王念博客 你好,想跟你请教个问题:我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.

加载中
0
大大吊炸天
大大吊炸天

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

0
Dreyer
Dreyer

过滤器中加这段代码

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    // 设置允许跨域
    HttpServletResponse response = (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);
}
0
guo11235
guo11235

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

我是这么加的 是可行的

0
抢小孩糖吃
抢小孩糖吃

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

@CrossOrigin
返回顶部
顶部