JQuery jsession cookie is not sent to a server

随声附和 提交于 2019-12-05 09:17:45

Indeed I do run Adblock Plus, (thanks for the tip) but it has nothing to do with this. Actually the thing is a bit tricky. I found two solutions, but I don't have explanation what is going on.

Solution #1: - saving JSESSIONID manually when page loads:

    var Session = {
       id : '${pageContext.session.id}',
       user : '${pageContext.request.remoteUser}'
    };

    $(window).load(function () {
        setCookie('JSESSIONID',Session.id);
    });

    function setCookie(name,value,expires,path,domain,secure) {
        var cookieString = name + "=" +escape(value) +
           ( (expires) ? ";expires=" + expires.toGMTString() : "") +
           ( (path) ? ";path=" + path : "") +
           ( (domain) ? ";domain=" + domain : "") +
           ( (secure) ? ";secure" : "");
        document.cookie = cookieString;
    }

This works fine and JSESSIONID gets send over in next ajax requests accordingly.

Solution #2: - Adding "text/plain" mime type in servlet doGet() method. Initially in my test I have not set any content type at all. After I realized that JQuery is expecting XML response by default, I changed my server doGet() method to set the content type explicitly like so:

resp.setContentType("text/plain");

Now, ajax call works fine too, and no manual cookie management is needed actually. I don't have explanation why Solution #2 works, but it is.

Thanks, D.

Check if you have any Ad Blocker installed in your browser. Many will strip out cookies from XHR.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!