Getting ASP.NET Cookieless Sessions and JQuery AJAX to Play Together

I have a website that uses jQuery AJAX. Using this jquery code

$.post("/ajax/getsomedata.aspx", {'id': id }, function(data) { dosomething(data); } ); 

When I run this with cookieless = "false" , the id is displayed in Request.Form . When I set cookieless = "true" , id is no longer in Request.Form .

UPDATE what i did

I added a call to Response.ApplyAppPathModifier () to save the data and avoid automatic redirection. I exclude ** Diago ((and delete my because its links give some idea of ​​what is going on. I like the idea of ​​a separate domain, but I cannot do it here.

Here's the updated code:

 $.post("<%=Response.ApplyAppPathModifier("/ajax/getsomedata.aspx")%>", {'id': id }, function(data) { dosomething(data); } ); 

According to MSDN, Response.ApplyAppPathModifier () adds a session identifier if you are in a state without a cookie session, and returns an immutable URL if you did not.

Since there is no session identifier, ASP.NET creates a new session and performs a redirect (thus deleting any form data).

+6
jquery ajax cookies session-state
source share
3 answers

I have been struggling with the same problem recently. This is one of the four disadvantages of using cookieless sessions. The request is rewritten when it hits the server, and the request variables are discarded. This can be very painful when using WebServices and POST requests using ASP.Net. This works great when using GET.

You will have a similar problem with regular mail. Here's an article that explains how to do this. This article on MSDN also addresses the flaws in detail.

The best solution is to host your services on a separate domain or IIS virtual site without using cookieless sessions.

Using cookie authentication is also an interesting task.

+2
source share

I spent all my day sorting out this problem, although this is just a quick fix.

During a PageMethod call, the PageMethod identifier is not passed with the request URL, so a new session_start event is session_start . We just need to set the exact request path before calling PageMethod so that a new session start event will not be fired.

 if ('<%= HttpContext.Current.Session.IsCookieless %>==True') { //need to pass session id in request path PageMethods.set_path('<%= System.Web.Configuration.WebConfigurationManager.AppSettings("WebRoot") %>(S(<%=Session.SessionID%>))/secure/PageName.aspx'); } PageMethods.PageMethodName(param1,param2, CallSuccess, CallFailed); 
+2
source share

You will need to include the session token in your request. ASP.Net should add it to URL fields or hidden forms. Take it and add a jquery parameter object. That, as far as I can tell, I never encoded in ASP.NET

0
source share

All Articles