reloading page while an ajax request in progress gives empty response and status as zero

Posted by Jayapal Chandran on Stack Overflow See other posts from Stack Overflow or by Jayapal Chandran
Published on 2010-11-17T06:33:17Z Indexed on 2010/12/24 3:54 UTC
Read the original article Hit count: 279

Filed under:

Hi,

Browser is firefox 3.0.10

I am requesting a page using ajax.

The response is in progress may be in readyState less than 4.

In this mean time i am trying to reload the page.

What happens is the request ends giving an empty response.

I used alert to find what string has been given as response text.

I assume that by this time the ready state 4 is reached.

why it is empty string.

when i alert the xmlhttpobject.status it displayed 0.

when i alert the xmlhttpobject.statusText an exception occurs stating that NOT AVAILABLE.

when i read in the document http://www.devx.com/webdev/Article/33024/0/page/2 it said for 3 and 4 status and statusText are available but when i tested only status is available but not satausText

Here is a sample code.

consider that i have requested a page and my callback function is as follows

function cb(rt)
{
   if(rt.readyState==4)
   {
      alert(rt.status);
      alert(rt.statusText); // which throws an exception
   }
}

and my server side script is as follows

sleep(30);
//flushing little drop down code

besides these i noticed the following...

assume again i am requesting the above script using ajax. now there will be an idle time till 30 seconds is over before that 30 seconds i press refresh. i got xmlhttpobject.status as 0 but still the browser did not reload the page untill that 30 seconds. WHY?

so what is happening when i refresh a page before an ajax request is complete is the status value is set to zero and the ready state is set to 4 but the page still waits for the response from the server to end...

what is happening...

THE REASON FOR ME TO FACE SOME THING LIKE THIS IS AS FOLLOWS.

when ever i do an ajax request ... if the process succeeded like inserting some thing or deleting something i popup a div stating that updated successfully and i will reload the page. but if there is any error then i do not reload the page instead i just alert that unable to process this request.

what happens if the user reloads the page before any of this request is complete is i get an empty response which in my calculation is there is a server error. so i was debugging the ajax response to filter out that the connection has been interrupted because the user had pressed reload. so in this time i don't want to display unable to process this request when the user reloads the page before the request has been complete.

oh... a long story. IT IS A LONG DESCRIPTION SO THAT I CAN MAKE EXPERTS UNDERSTAND MY DOUBT.

so what i want form the above.

any type of answer would clear my mind. or i would like to say all type of answers.

EDIT: 19 dec. If i did not get any correct answer then i would delete this question and will rewrite with examples. else i will accept after experimenting.

© Stack Overflow or respective owner

Related posts about AJAX