重新加载页面,而正在进行的Ajax请求给出空响应和状态为零 [英] Reloading page while an Ajax request in progress gives empty response and status as zero

查看:213
本文介绍了重新加载页面,而正在进行的Ajax请求给出空响应和状态为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览器是火狐3.0.10。我请求使用Ajax的页面。该反应过程中可能会在readyState的小于4。在此期间,我想重新加载页面。在请求结束,给人一种空的响应。

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.

我用警惕查找字符串已经给出响应文本。我认为这个时候的就绪状态4为止。为什么它是一个空字符串?

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?

当我提醒它显示0 xmlhttpobject.status。

When I alert the xmlhttpobject.status it displayed 0.

当我提醒发生异常的xmlhttpobject.statusText,称不可用。

When I alert the xmlhttpobject.statusText an exception occurs, stating "NOT AVAILABLE".

当我阅读文档 HTTP://www.devx。 COM / Webdev的/条/ 33024/0 /页/ 2 它说,3和4 状态状态文本是可用的,但是当我只测试状态可用,但不是satausText。

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.

下面是一个示例code。

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(30);
//flushing little drop down code

除了这些,我注意到以下几点。

Besides these I noticed the following.

假设我再次请求使用Ajax上面的脚本。 现在,将有30秒的空闲时间。 有30秒钟,然后我preSS刷新。 我xmlhttpobject.status为0,但仍是浏览器没有重新加载页面,直到30秒。为什么呢?

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?

当我刷新页面Ajax请求完成之前,状态值被设置为零和就绪状态被设置为4,但网页仍然等待来自服务器的响应,结束

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.

这是怎么回事?

对我来说,面临着一些这样的事如下。究其原因

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

每当我做一个Ajax请求,如果这个过程成功,如插入一些东西或删除一些东西我弹出一个div指出更新成功,我将重新加载页面。但是,如果有任何错误,那么我不刷新页面,而不是我只是提醒说无法处理此请求。

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.

如果用户重新加载页面之前,这些请求是完整的,我得到这在我的计算是有一个服务器错误空响应。所以我调试A​​jax响应以过滤掉的连接被中断,因为用户不得不pressed重装。所以这次我不想显示无法处理此请求当用户重新加载页面的请求之前已经完成了。

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.

编辑:12月19日 如果我没有没有得到任何正确的答案,然后我会删除这个问题,将结合实例改写。否则,我会尝试后接受的。

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.

我将创建一个演示程序,我会后这里的链接,这样我才能让你明白什么,我面对的问题。今天是29.dec.2010。

推荐答案

这已无关,与浏览器版本。

This has nothing to do with browser version.

浏览器检测页面的变化(如重装)它中止所有的 XHR 要求。每一个浏览器做到这一点。而且它的正经事要做。

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.

我的问题是:你为什么要重新加载页面时/ Ajax请求工作时

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

我的解决办法是:让所有的Ajax请求的跟踪和中止他们手动(是的,你可以这样做:的 https://developer.mozilla.org/en-US/docs/DOM/XMLHtt$p$pquest#abort()当你离开页面( onunload onbeforeunload的)。

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

这篇关于重新加载页面,而正在进行的Ajax请求给出空响应和状态为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆