Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Session Variable via AJAX - Not working 1

Status
Not open for further replies.

ElJayWilson

Programmer
Oct 31, 2008
19
US
I am having the most difficult time getting the value of a a session variable using AJAX. I am inheriting a classic ASP application and it uses session vars to keep track of key information. I am trying to get to those values using javascript and httprequest. It doesn't work consistently though.

Here is what I got:

Here is how I am making a call to my ajax.asp script and get the data back
Code:
// This will grab the value of a server session variable and return it
function getSessionVariable(id)
{
    // The server side script that will handle getting data
    var ajax_url="ajax/ajax.asp?cmd=get_session_variable&variable_name=" + id
   
	// Fire off the request and get something back
	ajaxrequest.open("GET", ajax_url, false);
	ajaxrequest.onreadystatechange = handlehttpresponse;
	ajaxrequest.send(null);
	//alert("SessionVar =" + ajaxrequest.responseText)
	try
    {
        // Evaluate the response and create the JSON object 
        var returnval = eval('('+ajaxrequest.responseText+')');
        return(returnval.Variable_Value);  // Return the session variable value
    }
    catch(err)
    {
        return("NOVALUE");
    }

}

Here is how it gets the value (or attempts to get the value) of the session variable (named passed in as argument) (ajax.asp)
Code:
 case "get_session_variable"
            dim variable_Name
            variable_Name = Request.QueryString("variable_name")
            strReturnVal = Session(variable_Name)
            
            Set member = jsObject() ' JSON rules
            member("Variable_Value") = strReturnVal
            member.Flush 'Writes out (returns) the object in JSON format to the calling script

Sometimes the session variable returns nothing, other times, it returns old session variable data.

What am I doing wrong? Is there a better way to get what I need?
 
Maybe this will help with troubleshooting. The code seems to work just fine on Firefox. I am using Firebug and it is returning the proper session variable value.

 
Solved. The problem was caching in IE. Didn't happen it Firefox.

Adding this before I do the ajaxrequest.open solved the issue.

Code:
ajaxrequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top