admin管理员组文章数量:1291120
How do I handle the scenario where I making a synchronous request to the server using XMLHttpRequest and the server is not available?
xmlhttp.open("POST","Page.aspx",false);
xmlhttp.send(null);
Right now this scenario results into a JavaScript error: "The system cannot locate the resource specified"
How do I handle the scenario where I making a synchronous request to the server using XMLHttpRequest and the server is not available?
xmlhttp.open("POST","Page.aspx",false);
xmlhttp.send(null);
Right now this scenario results into a JavaScript error: "The system cannot locate the resource specified"
Share Improve this question asked Dec 18, 2008 at 8:43 coder_brocoder_bro 10.8k14 gold badges60 silver badges90 bronze badges 1- geekswithblogs/lorint/archive/2006/03/07/71625.aspx heres a tutorial for doing it for a single request. – fasih.rana Commented Dec 18, 2008 at 9:20
3 Answers
Reset to default 3Ok I resolved it by using try...catch around xmlhttprequest.send
:
xmlhttp.open("POST","Page.aspx",false);
try
{
xmlhttp.send(null);
}
catch(e)
{
alert('there was a problem municating with the server');
}
Try the timeout property.
xmlHTTP.TimeOut= 2000
You don't check for properly returned status. By the code you gave you are doing a GET request. To properly check the status of your request, you must create an event handler for the onreadystatechange event and then inside it check if the readyState property is equal 4 and then inside the method if the status is 200.
You can find a detailed explanation here :Ajax Tutorial by Mozilla
xmlhttp.onreadystatechange=function()
xmlhttp.open("GET","Page.aspx",false);
{
if (xmlhttp.readyState==4)
{
if (xmlhttp.status==200)
{
//Ajax handling logic
}
}
}
xmlhttp.send(null);
本文标签: javascriptProblem XMLHttpRequesthandle server connection lostStack Overflow
版权声明:本文标题:javascript - Problem: XMLHttpRequest - handle server connection lost - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741526608a2383505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论