开发者

Reloading page while an Ajax request in progress gives empty response and status as zero

开发者 https://www.devze.com 2023-01-25 17:50 出处:网络
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 the meantime I am trying to reload the page. The request ends, giving an

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 the meantime I am trying to reload the page. 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 an empty string?

When I alert the xmlhttpobject.status it displayed 0.

When I alert the xmlhttpobject.statusText an exception occurs, stating "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 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开发者_C百科(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 of 30 seconds. Before that 30 seconds I press refresh. I got xmlhttpobject.status as, 0 but still the browser did not reload the page until that 30 seconds. Why?

When I refresh a page before an Ajax request is complete, 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.

Whenever 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.

If the user reloads the page before any of these requests is complete, 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 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 don't not get any correct answer then I will delete this question and will rewrite with examples. Else I will accept after experimenting.

I will create a demo program, and I will post the link here so that I can make you understand of what I am facing. Today is 29.dec.2010.


This has nothing to do with browser version.

The moment the browser detects a change in page (eg a reload) it aborts all XHR calls. Every browser does that. And it's the decent thing to do.

My question would be: why are you reloading the page when/while an ajax request is working?

My solution would be: keep track of all ajax requests and abort them 'manually' (yes, you can do that: https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#abort() when you leave the page (onunload or onbeforeunload).


You are using a pretty old version of Firefox there. 3.0.10 came out a year and a half ago. Lots has changed since then. There are some old Mozilla and Firefox bugs regarding this. They're closed and it looks like they were closed when Firefox 3 was in beta, but you never know, there may have been a regression. This is one possibility: https://bugzilla.mozilla.org/show_bug.cgi?id=511383

So, this is what I think is happening. I suspect that hitting reload causes any open http request to be aborted. Historically, Firefox hasn't handled abort too well - I think they intend for the readyState to be 0, but it cycles through 4 on the way there.

I recommend that you (and your users) upgrade your Firefox if possible. If that isn't possible, you'll have to work around this somehow. Testing for status == 0 and preventing your alert from firing would be a place to start. You could also set a flag in the onBeforeUnload event of window.


I had the same problem, it turned out to be the issue with PHP session. If the session is open, all other requests from the same session must wait.

To fix this you make sure you do not start the new session in your server-side scripts.

This might be a problem if you are using some framework and it starts the session automatically. In that case simply add this line at the beginning of all your long polling server-side scripts:

session_write_close();


I too have the same issue, and let me share with you what I have done to make it working. I have given the parameter global:false while framing ajax request. So, even if we navigate to any other page, with the ajax request on the fly, it will launch the page as desired.


I came across the same problem (in Chrome), and to me it seems like when I hit refresh in the middle of an xhr request the browser treats it as an error. So if we have specified an error block in our $.ajax call, the browser pops this as an error.

As a workaround I specified only a success block and the error disappeared. It looks like this is a scenario which should be handled by the browser itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消