AJAX请求挂起5分钟 [英] AJAX request hangs for 5 minutes
问题描述
我的网站对PHP文件使用大量AJAX请求,然后从MySQL检索
数据。大约95%的时间请求完成而没有
问题。每隔一段时间,一个请求就会挂起5分钟,然后就会失败。它似乎在客户端出现。我不能
重新创建问题。它只是随机发生的。我认为它最终会到达服务器(Apache),因为我可以在
服务器日志中看到某些POST / GET变量未定义的错误。
变量已明确定义。有一件事可能是因为我在某处看到POST向服务器发出两个
请求,因此它只与POST请求相关联。我有IE6和IE7的这个问题,可以将
绑定到我简要阅读的wininet.dll。我不确定。以下是我用于请求的典型结构。任何帮助
将不胜感激。谢谢...
函数loginReq(frm){
var req = false;
var msg = document.getElementById( " msg");
msg.innerHTML ="" ;;
msg.className =" error";
if( window.XMLHttpRequest){
req = new XMLHttpRequest();
} else if(window.ActiveXObject){
try {req = new ActiveXObject(" Msxml2.XMLHTTP");} catch(e){
try {req = new ActiveXObject(" Microsoft.XMLHTTP");} catch(e){}} >
}
if(!req){
msg.innerHTML ="请求有问题!" ;;
返回false;
}
req.onreadystatechange = function(){
if(req.readyState == 4){
if(req.status == 200){
if(req.responseText.substr(0,1)==''<''){
document.getElementById(" main")。innerHTML = req.resp onseText;
} else {
msg.innerHTML = req.responseText;
}
}否则{
msg.innerHTML ="请求有问题!" ;;
}
}
}
var prm =''user =''+ frm.user.value +''& passwd =''+ frm.passwd.v alue;
req.open(' 'POST'',''login.php'',true);
req.setRequestHeader(" Content-type"," application / x-www-form-
urlencoded");
req.setRequestHeader(" Content-length",prm.length);
req.setRequestHeader(" Connection","关闭);
req.send(prm);
}
< a href =mailto:mo ***** @ yahoo.com> mo ***** @ yahoo.com 写道:
我的网站对PHP文件使用了大量的AJAX请求,然后从MySQL检索了
数据。大约95%的时间请求完成而没有
问题。每隔一段时间,一个请求就会挂起5分钟,然后就会失败。它似乎在客户端出现。我不能
重新创建问题。它只是随机发生的。
读取就像竞争条件。
我认为它最终到达服务器(Apache)因为我可以在服务器日志中看到错误
,某些POST / GET变量未定义。
变量已明确定义。有一件事可能是因为我在某处看到POST向服务器发出两个
请求,因此它只与POST请求相关联。
你要么完全没读或完全误解了你的b
。一个(POST)请求仍然是* one *(POST)请求。
我在IE6和IE7上遇到此问题,可能与wininet.dll绑定/>
我也简要介绍了一下。我不确定。
在Windows上运行的其他UA也不得不使用wininet.dll吗?
< http: //msdn.microsoft.com/en-us/library/aa385483.aspx>
以下是我用于请求的典型结构。任何帮助将
表示赞赏。谢谢...
函数loginReq(frm){
[...]
if(window.XMLHttpRequest){
^^^^^^^^^^^^^^^^^^^^^
req = new XMLHttpRequest();
} else if(window.ActiveXObject){
^^^^^^^^ ^^^^^^^^^^^^^
这些功能测试都不够。例如,并非每个属性都可以调用
。而且你不应该依赖于`window''属性来引用全局对象的
。阅读常见问题解答并在两者上搜索档案。
try {req = new ActiveXObject(" Msxml2.XMLHTTP");} catch(e){
try {req = new ActiveXObject(" Microsoft.XMLHTTP");} catch(e){}}
}
测试Microsoft.XMLHTTP通常就足够了。
if(!req){
msg.innerHTML ="请求有问题!;
返回false;
}
req.onreadystatechange = function(){
[...]
}
var prm =''user =''+ frm.user.value +''& passwd ='' + frm.passwd.v alue;
如果你声明`Content-type:application / x-www-form-urlencoded'',你必须要求
转义那些值您使用RFC3986中定义的百分比编码作为URI组件或在邮件正文中使用
。实现
ECMAScript Ed。 3为此提供了encodeURIComponent()方法;对于较旧的实现,你需要使用escape()
甚至可能是一个虚拟方法,因为
后备。
req.open(''POST'',''login.php'',true);
该调用必须在*指定给'onreadystatechange''属性之前*
req .setRequestHeader(" Content-type"," application / x-www-form-
urlencoded");
此调用不是普遍支持的,您需要对其进行功能测试,并且
捕获它可能抛出的异常。无论如何,它可能是多余的。
req.setRequestHeader(" Content-length",prm.length);
req。 setRequestHeader(QUOT;连接与QUOT;,"靠近");
这些调用毫无意义,实现可能会忽略它们。
req.send(prm) ;
}
HTH
PointedEars
-
var bugRiddenCrashPronePieceOfJunk =(
navigator.userAgent.indexOf(''MSIE 5'')!= -1
&& navigator .userAgent.indexOf(''Mac'')!= -1
)// Plone,register_function.js:16
function loginReq (frm){
[...]
* if(window.XMLHttpRequest){
* * * * ^^^^^^^^^^^^^^^^^^^^^ * * req = new XMLHttpRequest();
*}否则if(window.ActiveXObject){
* * * * * * * ^ ^^^^^^^^^^^^^^^^^^^
这些功能测试都不够。 *例如,并非每个属性都可以调用
。 *你不应该依赖'window''属性来引用全局对象
。 *阅读常见问题解答并在两者上搜索档案。
如果您指的是< http://www.jibbering.com/faq/ > ;,我不能'$
找到任何告诉我们不要依赖''window''属性的东西
指的是Global Object。如果我错了,请纠正我,但如果对于全局XMLHttpRequest属性的测试成功,我们几乎可以确保它是一个函数。
总是确定它是一个函数。
* req.open(''POST'',''login.php'',true);
该调用必须在*指定给'onreadystatechange''属性之前*。
XMLHttpRequest的工作草案说它是在你分配
onreadystatechange属性之后发生的。
< http://www.w3.org/TR/XMLHttpRequest/ > ;
* req.setRequestHeader(" Content-type"," application / x-www-form-
urlencoded");
此调用不受普遍支持,您需要对其进行功能测试,并且
捕获它可能抛出的异常。 *无论如何,它可能是多余的。
虽然XMLHttpRequest仍然没有标准化,但是每个实现并不是每个实现提供一个setRequestHeader函数吗?
>
* req.setRequestHeader(" Content-length",prm.length);
* req.setRequestHeader(" Connection" ,"靠近");
这些调用毫无意义,实现可能会忽略它们。
当
发出HTTP请求时,浏览器使用的默认连接请求标头是保持活动,所以如果XHR实现/
HTTP客户端选择忽略覆盖默认值的那个,
不会有点错吗?
9月14日下午2:15 *,Thomas''PointedEars''Lahn< PointedE ... @ web.de>
写道:
sasuke写道:
当
$时,浏览器使用的默认连接请求标头b $ b发出HTTP请求是''Keep-Alive''
废话。 *显然你不知道你在说什么。 *阅读
RFC 1945和2616,请不要在此之前继续。
我想不是每个浏览器都在那里,但至少FF工作
这样。使用嗅探工具捕获Request标头在
最少说。但你是对的,这不是授权。
所以如果XHR实现/ HTTP客户端选择忽略
一个覆盖默认值,不会有点错吗?
非sequitur。 *为什么不简单地测试我的建议? * BTDT。
有人想知道为什么你在这里问你是否认为你知道的更好。
请从我之前的帖子中指出一个段落,表明我的
声称知道一切。我知道你比我更清楚因此
这些问题不是''问号''而是真正的''查询''。
向每个人劫持线程表示歉意;很好奇。不过,
无论如何,谢谢你的澄清。
问候,
/ sasuke。
My site uses lots of AJAX requests to PHP files which then retrieves
data from MySQL. About 95% of the time the requests complete without
issues. Every once in a while a request will hang for exactly 5
minutes then fail. It seems to be occuring on the client side. I
can''t re-create the issue. It just happens randomly. I think it
eventally reaches the server (Apache) because I can see errors in the
server log that certain POST/GET variables are undefined. The
variables are clearly defined. One thing it could be is that it''s
only tied to POST requests since I read somewhere that POST makes two
requests to the server. I have this issue with IE6 and IE7 and could
be tied to the wininet.dll I briefly read about as well. I''m not
sure. Below is the typical structure I use for my requests. Any help
will be appreciated. Thanks...
function loginReq(frm){
var req=false;
var msg=document.getElementById("msg");
msg.innerHTML="";
msg.className="error";
if(window.XMLHttpRequest){
req=new XMLHttpRequest();
}else if(window.ActiveXObject){
try{req=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
try{req=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}
}
if(!req){
msg.innerHTML="There was a problem with the request!";
return false;
}
req.onreadystatechange=function(){
if(req.readyState==4){
if(req.status==200){
if(req.responseText.substr(0,1)==''<''){
document.getElementById("main").innerHTML=req.resp onseText;
}else{
msg.innerHTML=req.responseText;
}
}else{
msg.innerHTML="There was a problem with the request!";
}
}
}
var prm=''user=''+frm.user.value+''&passwd=''+frm.passwd.v alue;
req.open(''POST'',''login.php'',true);
req.setRequestHeader("Content-type","application/x-www-form-
urlencoded");
req.setRequestHeader("Content-length",prm.length);
req.setRequestHeader("Connection","close");
req.send(prm);
}
mo*****@yahoo.com wrote:My site uses lots of AJAX requests to PHP files which then retrieves
data from MySQL. About 95% of the time the requests complete without
issues. Every once in a while a request will hang for exactly 5
minutes then fail. It seems to be occuring on the client side. I
can''t re-create the issue. It just happens randomly.Reads like a race condition.
I think it eventally reaches the server (Apache) because I can see errors
in the server log that certain POST/GET variables are undefined. The
variables are clearly defined. One thing it could be is that it''s
only tied to POST requests since I read somewhere that POST makes two
requests to the server.Either you have read utter nonsense or misunderstood completely what you had
read. One (POST) request is still *one* (POST) request.
I have this issue with IE6 and IE7 and could be tied to the wininet.dll
I briefly read about as well. I''m not sure.Wouldn''t other UAs running on Windows have to use wininet.dll as well?
<http://msdn.microsoft.com/en-us/library/aa385483.aspx>
Below is the typical structure I use for my requests. Any help will
be appreciated. Thanks...
function loginReq(frm){
[...]
if(window.XMLHttpRequest){^^^^^^^^^^^^^^^^^^^^^
req=new XMLHttpRequest();
}else if(window.ActiveXObject){^^^^^^^^^^^^^^^^^^^^
Those are hardly sufficient feature tests. For example, not every property
can be called. And you should not rely on the `window'' property to refer to
the Global Object. Read the FAQ and search the archives on both.
try{req=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
try{req=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}
}Testing for `Microsoft.XMLHTTP'' usually suffices.
if(!req){
msg.innerHTML="There was a problem with the request!";
return false;
}
req.onreadystatechange=function(){
[...]
}
var prm=''user=''+frm.user.value+''&passwd=''+frm.passwd.v alue;If you declare `Content-type: application/x-www-form-urlencoded'', you MUST
escape the values that you use as an URI component or in the message body,
using the percent encoding defined in RFC3986. Implementations of
ECMAScript Ed. 3 provide the encodeURIComponent() method for that purpose;
for older implementations you need escape() and maybe even a dummy method as
fallbacks.
req.open(''POST'',''login.php'',true);That call must come *before* you assign to the `onreadystatechange'' property.
req.setRequestHeader("Content-type","application/x-www-form-
urlencoded");This call is not universally supported, you need to feature-test it and
catch the exceptions it may throw. It is likely to be superfluous anyway.
req.setRequestHeader("Content-length",prm.length);
req.setRequestHeader("Connection","close");These calls are pointless, the implementation will likely ignore them.
req.send(prm);
}
HTH
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16
function loginReq(frm){[...]
* if(window.XMLHttpRequest){
* * * *^^^^^^^^^^^^^^^^^^^^^* * req=new XMLHttpRequest();* }else if(window.ActiveXObject){
* * * * * * *^^^^^^^^^^^^^^^^^^^^
Those are hardly sufficient feature tests. *For example, not every property
can be called. *And you should not rely on the `window'' property to refer to
the Global Object. *Read the FAQ and search the archives on both.If you are referring to < http://www.jibbering.com/faq/ >, I couldn''t
find anything which tells us not to rely on the ''window'' property to
refer to Global Object. Correct me if I am wrong but if the test for
the Global XMLHttpRequest property is successful, we can be almost
always sure that it will be a function.
* req.open(''POST'',''login.php'',true);
That call must come *before* you assign to the `onreadystatechange'' property.The working draft of XMLHttpRequest says it comes after you assign the
onreadystatechange property.
< http://www.w3.org/TR/XMLHttpRequest/ >
* req.setRequestHeader("Content-type","application/x-www-form-
urlencoded");
This call is not universally supported, you need to feature-test it and
catch the exceptions it may throw. *It is likely to be superfluous anyway.Though XMLHttpRequest is still not standardized, doesn''t each and
every implementation out there provide a setRequestHeader function?
>* req.setRequestHeader("Content-length",prm.length);
* req.setRequestHeader("Connection","close");
These calls are pointless, the implementation will likely ignore them.The default ''Connection'' request header used by the browser when
issuing HTTP Requests is ''Keep-Alive'' so if the XHR implementation /
HTTP Client chooses to ignore the one which overrides the default,
wouldn''t it be kind of wrong?
On Sep 14, 2:15*pm, Thomas ''PointedEars'' Lahn <PointedE...@web.de>
wrote:sasuke wrote:The default ''Connection'' request header used by the browser when
issuing HTTP Requests is ''Keep-Alive''
Nonsense. *Obviously you have no idea what you are talking about. *Read
RFCs 1945 and 2616, and please don''t continue here before you did.I guess not on each and every browser out there but at least FF works
this way. Trapping the Request headers with a sniffing tool says so at
least. But you are right, it''s not a mandate.
so if the XHR implementation / HTTP Client chooses to ignore the
one which overrides the default, wouldn''t it be kind of wrong?
Non sequitur. *Why don''t you simply test my advice? *BTDT.
One wonders why you asked here if you think you know everything better.Kindly point me a passage from my previous post which indicates my
claiming to know everything. I know you know better than me hence
those questions were not ''question tags'' but genuine ''queries''.
Apologies to everyone for hijacking the thread; was curious. Still,
anyways, thanks for the clarifications.
Regards,
/sasuke.
这篇关于AJAX请求挂起5分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!