473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

AJAX not working when using Session variables.

Greetings! I was researching AJAX to provide a solution to displaying status
messages while a long process executed. I found several examples online and
was able to use their code to get a quick application working. However, when
attempting to implement the solution, the AJAX calls weren't updating the
screen like the examples were and seemed not to fire until after the long
running process had completed.

I found the only real difference between my application and the quick app is
that I am using Session variables. I added a Session variable to the quick
app and was able to duplicate the behavior.

The quick app I wrote is simple in nature, when the page loads, it begins
making AJAX calls and updates a div on the page with the current time every
second. A button on the page issues a sleep command on the thread for 10
seconds. When not using a Session variable, the time continues to update
after the button is pressed. When using a session variable, the time ceases
to update until the sleep command has expired.

Is there any way to get AJAX to work when using Session variables?
May 31 '07 #1
6 5110
KJ
Can you please post the relevant code?

"Shawn Sesna" <Sh********@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
Greetings! I was researching AJAX to provide a solution to displaying
status
messages while a long process executed. I found several examples online
and
was able to use their code to get a quick application working. However,
when
attempting to implement the solution, the AJAX calls weren't updating the
screen like the examples were and seemed not to fire until after the long
running process had completed.

I found the only real difference between my application and the quick app
is
that I am using Session variables. I added a Session variable to the
quick
app and was able to duplicate the behavior.

The quick app I wrote is simple in nature, when the page loads, it begins
making AJAX calls and updates a div on the page with the current time
every
second. A button on the page issues a sleep command on the thread for 10
seconds. When not using a Session variable, the time continues to update
after the button is pressed. When using a session variable, the time
ceases
to update until the sleep command has expired.

Is there any way to get AJAX to work when using Session variables?

May 31 '07 #2
Below is all the code you'll need to duplicate the problem.
DEFAULT.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript"
src="javascript/Ajax.js"></script>
<script type="text/javascript" language="javascript">
setTimeout("blah();", 1000);
function clicky() {
blah();
}

function clicky2() {
var a = new Ajax('default.aspx?blah2=a', '', null);
a.returnType = 'JSON';
a.send();
}

function blah() {
var a = new Ajax('default2.aspx?blah=a', '', null);
a.returnType = 'JSON';
a.send();

setTimeout("blah();", 1000);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="updater">

</div>
<div id="updater2">

</div>
<!--<input type="button" onclick="clicky();" value="Start One Second
Timer" />-->
<asp:Button id="blahbutton" runat="server" OnClick="blah_click" Text="Do
One-Time 3 Second Call" />
<!--<input type="button" onclick="clicky2();" value="Do One-Time 3
Second Call" />-->
</form>
</body>
</html>

DEFAULT.ASPX.CS

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
Response.Write("POSTED!<br>");
//Session["hi"] = "hi"; <--- UNCOMMENT TO DUPLICATE PROBLEM
}

protected void blah_click(object sender, EventArgs e)
{

System.Threading.Thread.Sleep(10000);
}
}

DEFAULT2.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"
Inherits="Default2" %>

DEFAULT2.ASPX.CS

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["blah"] != null)
{
Response.Write("var a =
document.getElementById('updater');while(a.childNo des.length >
0){a.removeChild(a.childNodes[0]);}a.appendChild(document.createTextNode('");
if (Application["a"] == null)
Response.Write(DateTime.Now.ToString());
else
Response.Write(Application["a"].ToString());
Response.Write("'));");
Response.End();
}

}
}

AJAX.JS

function Ajax(url, requestXml, callback, sendNow) {
this.url = url;
this.requestMethod = "POST";
this.requestXml = requestXml;
this.callback = callback;
this.async = true;

this.returnType = null;
this.setReturnType = __Ajax_SetReturnType;

this.errorCallback = null;
this.timeoutCallback = null;

this.request = null;

this.send = __Ajax_Send;

if(this.url && this.requestXml && this.callback && sendNow != undefined &&
sendNow)
this.send();
}

Ajax.CreateRequest = __Ajax_CreateRequest;

function __Ajax_CallbackIntercept(instance) {
if(instance.request.readyState == 4) {
if(instance.request.status && instance.request.status == 408 &&
instance.timeoutCallback)
instance.timeoutCallback(instance);
else if(instance.request.status && instance.request.status == 200) {
if(instance.returnType == null && instance.callback)
instance.callback(instance);
else if(typeof(instance.returnType) == "string")
if(instance.returnType == "JSON") {
var json = eval(instance.request.responseText);

if(instance.callback)
instance.callback(json, instance);
} else if(instance.returnType == "TEXT") {
instance.callback(instance.request.responseText, instance);
} else if(instance.callback)
instance.callback(instance.request.responseXML, instance);
else {
if(instance.replaceContents) {
while(instance.idOrDomObjectToInsertInto.childNode s.length 0)
instance.idOrDomObjectToInsertInto.removeChild(ins tance.idOrDomObjectToInsertInto.childNodes[0]);
}

instance.idOrDomObjectToInsertInto.appendChild(doc ument.createTextNode(instance.request.responseText ));

if(instance.callback)
instance.callback(instance.request.responseText);
}
} else {
if(instance.errorCallback)
instance.errorCallback(instance);
}
}
}

function __Ajax_Send() {
var instance = this;
// alert('hey');
//alert(this.request);
this.request = __Ajax_CreateRequest();
if(this.request.overrideMimeType)
this.request.overrideMimeType("text/xml");
this.request.onreadystatechange = function() {
__Ajax_CallbackIntercept(instance);
}

this.request.open(this.requestMethod, this.url, this.async);
this.request.send(this.requestXml);
}

//returnType values = "JSON", "XML", "TEXT", "HTML", "XHTML"
function __Ajax_SetReturnType(returnType, idOrDomObjectToInsertInto,
replaceContents) {
this.returnType = returnType;
this.idOrDomObjectToInsertInto = idOrDomObjectToInsertInto;
this.replaceContents = replaceContents;

if(typeof(this.idOrDomObjectToInsertInto) == "string")
this.idOrDomObjectToInsertInto =
document.getElementById(idOrDomObjectToInsertInto) ;
}

function __Ajax_CreateRequest() {
var request = null;

try {
request = new XMLHttpRequest();
} catch(e) {}

if(!request && window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");

return request;
}
"KJ" wrote:
Can you please post the relevant code?

"Shawn Sesna" <Sh********@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
Greetings! I was researching AJAX to provide a solution to displaying
status
messages while a long process executed. I found several examples online
and
was able to use their code to get a quick application working. However,
when
attempting to implement the solution, the AJAX calls weren't updating the
screen like the examples were and seemed not to fire until after the long
running process had completed.

I found the only real difference between my application and the quick app
is
that I am using Session variables. I added a Session variable to the
quick
app and was able to duplicate the behavior.

The quick app I wrote is simple in nature, when the page loads, it begins
making AJAX calls and updates a div on the page with the current time
every
second. A button on the page issues a sleep command on the thread for 10
seconds. When not using a Session variable, the time continues to update
after the button is pressed. When using a session variable, the time
ceases
to update until the sleep command has expired.

Is there any way to get AJAX to work when using Session variables?


May 31 '07 #3
It works but this is likely because you perform concurrent ajax calls. When
a request is processed and session state is enabled, there is a lock so that
the request can access to the session state without having another request
on the same sesion state messing things up...

Do you actually need to read/write session variables ? You could perhaps use
a directive to disable this if not needed. You could also perhaps try to
queue AJAX requests so that you don't have competing AJAX requests at the
same time (or just avoid this design if you can do so)...

---
Patrice

"Shawn Sesna" <Sh********@discussions.microsoft.coma écrit dans le message
de news: 9C**********************************@microsoft.com...
Greetings! I was researching AJAX to provide a solution to displaying
status
messages while a long process executed. I found several examples online
and
was able to use their code to get a quick application working. However,
when
attempting to implement the solution, the AJAX calls weren't updating the
screen like the examples were and seemed not to fire until after the long
running process had completed.

I found the only real difference between my application and the quick app
is
that I am using Session variables. I added a Session variable to the
quick
app and was able to duplicate the behavior.

The quick app I wrote is simple in nature, when the page loads, it begins
making AJAX calls and updates a div on the page with the current time
every
second. A button on the page issues a sleep command on the thread for 10
seconds. When not using a Session variable, the time continues to update
after the button is pressed. When using a session variable, the time
ceases
to update until the sleep command has expired.

Is there any way to get AJAX to work when using Session variables?

May 31 '07 #4
when you use session, asp.net queues the requests to the same session,
to implement session locking, so your analysis of the problem is correct.

just turn off session on the ajax request page (use a web service)

-- bruce (sqlwork.com)

Shawn Sesna wrote:
Greetings! I was researching AJAX to provide a solution to displaying status
messages while a long process executed. I found several examples online and
was able to use their code to get a quick application working. However, when
attempting to implement the solution, the AJAX calls weren't updating the
screen like the examples were and seemed not to fire until after the long
running process had completed.

I found the only real difference between my application and the quick app is
that I am using Session variables. I added a Session variable to the quick
app and was able to duplicate the behavior.

The quick app I wrote is simple in nature, when the page loads, it begins
making AJAX calls and updates a div on the page with the current time every
second. A button on the page issues a sleep command on the thread for 10
seconds. When not using a Session variable, the time continues to update
after the button is pressed. When using a session variable, the time ceases
to update until the sleep command has expired.

Is there any way to get AJAX to work when using Session variables?
May 31 '07 #5
Thank you to all that answered, this is very useful information.

"bruce barker" wrote:
when you use session, asp.net queues the requests to the same session,
to implement session locking, so your analysis of the problem is correct.

just turn off session on the ajax request page (use a web service)

-- bruce (sqlwork.com)

Shawn Sesna wrote:
Greetings! I was researching AJAX to provide a solution to displaying status
messages while a long process executed. I found several examples online and
was able to use their code to get a quick application working. However, when
attempting to implement the solution, the AJAX calls weren't updating the
screen like the examples were and seemed not to fire until after the long
running process had completed.

I found the only real difference between my application and the quick app is
that I am using Session variables. I added a Session variable to the quick
app and was able to duplicate the behavior.

The quick app I wrote is simple in nature, when the page loads, it begins
making AJAX calls and updates a div on the page with the current time every
second. A button on the page issues a sleep command on the thread for 10
seconds. When not using a Session variable, the time continues to update
after the button is pressed. When using a session variable, the time ceases
to update until the sleep command has expired.

Is there any way to get AJAX to work when using Session variables?
May 31 '07 #6
Web service does indeed work. I added a web service to the application and
all AJAX calls update. Thank you very much for your help :)

"bruce barker" wrote:
when you use session, asp.net queues the requests to the same session,
to implement session locking, so your analysis of the problem is correct.

just turn off session on the ajax request page (use a web service)

-- bruce (sqlwork.com)

Shawn Sesna wrote:
Greetings! I was researching AJAX to provide a solution to displaying status
messages while a long process executed. I found several examples online and
was able to use their code to get a quick application working. However, when
attempting to implement the solution, the AJAX calls weren't updating the
screen like the examples were and seemed not to fire until after the long
running process had completed.

I found the only real difference between my application and the quick app is
that I am using Session variables. I added a Session variable to the quick
app and was able to duplicate the behavior.

The quick app I wrote is simple in nature, when the page loads, it begins
making AJAX calls and updates a div on the page with the current time every
second. A button on the page issues a sleep command on the thread for 10
seconds. When not using a Session variable, the time continues to update
after the button is pressed. When using a session variable, the time ceases
to update until the sleep command has expired.

Is there any way to get AJAX to work when using Session variables?
May 31 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Larry Woods | last post by:
I have a site that works fine for days, then suddenly, I start getting ASP 0115 errors with an indication that session variables IN SEPARATE SESSIONS have disappeared! First, for background...
0
by: melledge | last post by:
Ajax Developers' Day to Kick Off XTech 2006 Conference Industry experts offer insight into next generation of the Web ALEXANDRIA, VIRGINIA, USA - April 25, 2006 - In response to the rapidly...
5
by: dougwig | last post by:
I'm trying to handle the scenario where a user's session times out and and their ajax request triggers a redirection by the webserver (302 error?). I'm using Prototype 1.4 and the my works great...
3
by: vunet.us | last post by:
Hello, I am breaking my head running out of ideas about the best solution to my goal. I want to load some pages generated with the server (ASP) and assign their html results to JavaScript, so...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
13
by: Marvin Zhang | last post by:
Hi, I'm not familiar with web programming, but I have a problem here. I have a page. When a user click one button on it, I will use AJAX to request a PHP script which will do a bunch of tasks,...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
16
by: deostroll | last post by:
Suppose I have an asp page that has a response.write(something) in a loop that would run for a considerable amount of time. Now, from my client browser can I trap those server response messages...
0
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.