Solving this issue has been an nightmare for me but finally i have
achieve it some how, this may be not a full proof solution but it
worked for me. This solution also has certain flaws which i also will
be discuss in this article.
Session TimeOut happen due to various reasons such
as Server clearing the session cookie after timeout period, user
clearing the cache content from browser, Context Reload at Server etc.
But if there is session timeout happens before making Ajax
Call the client machine may get abrupt error page if you are not
trapping HTTP Status Code, this may not be due to Ajax call but session
has cleared and the server id not able to process the requested page.
What i found is the data i am returning data from the Ajax
Call is an String HTML content that i want to show and whenever some
thing happens at server end like session timeout the response i
received from Server contains HTTP Status Code 500 message error. Under
normal Ajax call i will not receive the HTTP Status Message so what i
did is to check whether the response received from server contains HTTP Status Code 500.
If it contains HTTP Status code i know that there is some unknown
server error occurred and i am able to trap error occurred during Ajax
Call.
Ajax Code
<script type="text/javascript">var xmlhttp = new getXMLObject();var time_variable; function getXMLObject() //XML OBJECT{ var xmlHttp = false; try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+ } catch (e2) { xmlHttp = false // No Browser accepts the XMLHTTP Object then false } } if (!xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers } return xmlHttp; // Mandatory Statement returning the ajax object created} function ajaxFunction() { var getdate = new Date(); //Used to prevent caching during ajax call if(xmlhttp) { xmlhttp.open("GET","ajax_time.php?" + getdate.getTime(),true); xmlhttp.onreadystatechange = handleServerResponse; xmlhttp.send(null); }} function handleServerResponse() { if (xmlhttp.readyState == 4) { if(xmlhttp.status == 200) { document.myForm.time.value=xmlhttp.responseText; //Update the HTML Form element } else { if(xmlhttp.status == 500) { alert("Session TimeOut. Unable To Process Your Request"); //Process Further } As you can see you have made an successfully Ajax Call to the requestedpage so you receive readyState = 4 but during session timeOut thestatus code received from requested page is 500 and not 200 so now youcan write your code logic to handle session timeOut Error. } }}</script>
Sign up for our daily email newsletter:
You must log in to post a comment.