本文介绍了JQuery jsession cookie不会发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我搜索过jquery forum,stackoverflow,google,bing,甚至yahoo w / o成功。 每10秒我试图通过这个JQuery片段从logservlet servlet加载文本数据: < script type =text / javascriptsrc =http://code.jquery.com/jquery-1.7.2.min.js>< / script> < script type =text / javascript> var autorefresh = setInterval(function(){ $ .ajax({ url:logservlet?devkey = chat, success:function(data){ $(#log_ta)。append(data); } }); },10000); < / script> 问题是,在服务器端我看不到有效的会话,会话属性。问题似乎与在JQuery的http请求的头中缺少Cookie JSESSIONID = xxxxxxxxxxxxxxxxxxxxxxxxxxx相关。我从服务器获取响应头,其中JSESSIONID总是随着每个请求而变化: 服务器Apache-Coyote / 1.1 Set-Cookie JSESSIONID = 9EEAFA2A933E7742D8FEDADD5345B76D; Path = / CumulusServer Content-Length 0 Date Fri,23 Mar 2012 13:02:08 GMT 但是JQuery不会随后使用它,这里是请求头: 主机192.168.1.11 :8080 用户代理Mozilla / 5.0(Windows NT 5.1; rv:5.0)Gecko / 20100101 Firefox / 5.0 接受* / * 接受语言en-us,en; q = 0.5 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8; q = 0.7,*; q = 0.7 连接保持活动 X -Requested-With XMLHttpRequest Referer http://192.168.1.11:8080//CumulusServer/ 这里有什么问题? $ .ajax调用不能识别会话吗?或者,我错过了一些管道代码在每个ajax请求中手动硬编码JSESSIONID?是这样,这应该是什么样子? Btw,当我从网络浏览器调用相同的url JSESSIONID头被发送到服务器! 谢谢, D。解决方案确实我运行Adblock Plus,(感谢提示),但它与这无关。 其实这件事有点棘手。我找到了两个解决方案,但我没有解释发生了什么。 解决方案#1: - 在网页加载时手动保存JSESSIONID : var Session = { id:'$ {pageContext.session.id}', user:'$ {pageContext.request.remoteUser}'}; $(window).load(function(){ setCookie('JSESSIONID',Session.id); }); 函数setCookie(name,value,expires,path,domain,secure){ var cookieString = name +=+ escape(value)+ ?; expires =+ expires.toGMTString():)+ ((path)?; path =+ path:)+ =+ domain:)+ ((secure)?; secure:); document.cookie = cookieString; } 这样工作正常,JSESSIONID在下一个ajax请求中发送。解决方案#2: - 在servlet doGet()方法中添加text / plainmime类型。p> 最初在我的测试中,我没有设置任何内容类型。在我意识到JQuery默认情况下期望XML响应,我改变了我的服务器doGet()方法来显式设置内容类型如下: resp.setContentType / plain); 现在,ajax调用也很好,实际上不需要手动进行cookie管理。我没有解释为什么解决方案#2工作,但它是。 谢谢, D。 I've searched jquery forum, stackoverflow, google, bing, and even yahoo w/o success.Every 10 sec I'm trying to load a text data from logservlet servlet via this JQuery snippet: <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <script type="text/javascript"> var autorefresh = setInterval(function () { $.ajax({ url : "logservlet?devkey=chat", success : function(data) { $("#log_ta").append(data); } }); }, 10000); </script>The problem is, on a server side I don't see a valid session where I try to track the session attributes. The problem seems to be related to missing "Cookie JSESSIONID=xxxxxxxxxxxxxxxxxxxxxxxxxxx" in the header of the http requests from JQuery. I'm getting response headers from the server, with JSESSIONID always changing with each request: Server Apache-Coyote/1.1 Set-Cookie JSESSIONID=9EEAFA2A933E7742D8FEDADD5345B76D; Path=/CumulusServer Content-Length 0 Date Fri, 23 Mar 2012 13:02:08 GMTBut JQuery doesn't use it subsequently, here are the request headers: Host 192.168.1.11:8080 User-Agent Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Accept */* Accept-Language en-us,en;q=0.5 Accept-Encoding gzip, deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection keep-alive X-Requested-With XMLHttpRequest Referer http://192.168.1.11:8080//CumulusServer/What's the issues here? Are "$.ajax" calls not session aware? Or do I miss some plumbing code to hardcode the JSESSIONID manually in each ajax request? Is so, how this should look like? Btw, when I call the same url from web browser the JSESSIONID header is sent to the server!Thanks,D. 解决方案 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. 这篇关于JQuery jsession cookie不会发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-30 07:59