当我使用XMLHttpRequest发起同步请求时,如果服务器没有响应,该如何处理?
xmlhttp.open("POST","Page.aspx",false);
xmlhttp.send(null);
目前这种情况会导致JavaScript错误:"系统无法找到指定的资源"
当我使用XMLHttpRequest发起同步请求时,如果服务器没有响应,该如何处理?
xmlhttp.open("POST","Page.aspx",false);
xmlhttp.send(null);
目前这种情况会导致JavaScript错误:"系统无法找到指定的资源"
xmlhttp.open("POST","Page.aspx",false);
try
{
xmlhttp.send(null);
}
catch(e)
{
alert('there was a problem communicating with the server');
}
尝试使用timeout属性。
xmlHTTP.TimeOut= 2000
你没有检查返回状态是否正确。根据你提供的代码,你正在执行一个GET请求。 为了正确检查请求的状态,你必须为onreadystatechange事件创建一个事件处理程序,然后在其中检查readyState属性是否等于4,然后在方法内部检查状态是否为200。
你可以在这里找到详细的解释:Mozilla的Ajax教程
xmlhttp.onreadystatechange=function()
xmlhttp.open("GET","Page.aspx",false);
{
if (xmlhttp.readyState==4)
{
if (xmlhttp.status==200)
{
//Ajax handling logic
}
}
}
xmlhttp.send(null);