跨域请求设置

来源:互联网 发布:13是什么意思网络语言 编辑:程序博客网 时间:2024/06/11 18:16

js设置:

<script>
    $(function () {  //, headers: { 'x-requested-with': 'XMLHttpRequest' }
        $.ajaxSetup({crossDomain: true, xhrFields: {withCredentials: true}});
    });
</script>

后端设置:

private boolean recharge(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String url = request.getHeader("Origin");
        logger.debug("Access-Control-Allow-Origin:" + url);
        if (!StringUtils.isEmpty(url)) {
            String val = response.getHeader("Access-Control-Allow-Origin");
            if (StringUtils.isEmpty(val)) {
                response.addHeader("Access-Control-Allow-Origin", url);
                response.addHeader("Access-Control-Allow-Credentials""true");
            }
        }
        return true;
    }
完成后 客户端跨域访问时可以携带cookie 服务器端也会响应 此时session不会失效

原创粉丝点击