2016-09-27 63 views
0

我有一个PHP页面,其形式为,其中有100个字段。我使用Ajax在的数据中输入内容到每个字段。阿贾克斯形式超时

有时,它发生,我得到这个错误:

net::ERR_CONNECTION_TIMED_OUT

大概是因为服务器不能及时响应。发生这种情况时,出现错误,Ajax将无法继续工作。

有没有一种方法可以让代码在出错后执行?这样我可以继续尝试Ajax吗?

注意:我使用的是老式的XMLHttpRequest()要求,而不是jQuery函数

+0

请发表您的代码 - 这样有”在这里实际上是错误的猜测 – eithed

回答

1

在您的Ajax代码尝试定义超时设置为0(无限制):

$.ajax({ 
timeout: 0, //Set your timeout value in milliseconds or 0 for unlimited 

您可以将此值设置为3秒(3000)并使用错误函数捕获异常。就像这样:

error: function(jqXHR, textError, errorThrown) { 
    if(textError==="timeout") { 
     alert("Call has timed out"); //Handle the timeout 
    } else { 
     alert("Unknown error"); //Handle other error type 
    } 

希望这将有助于

+0

我没有使用jquery,我使用普通的旧XMLHttpRequest(),我会尝试使用超时与该https://developer.mozilla.org/en-US/docs/ Web/API/XMLHttpRequest/timeout –

+0

据我所知,添加超时只会决定多长时间g脚本应等待响应,如果响应失败,脚本是否会停止工作?或者它会采取下一个Ajax请求,并继续他们?如果发生错误,Chrome似乎会取消所有的脚本执行,有没有办法在Chrome中绕过它? –

+0

您可以通过以下链接访问chrome事件:chrome:// net-internals /#events 查看为什么被取消 –

-1

您可以使用此功能来捕获所有样的错误,你可以在阿贾克斯获得:

$.ajax({ 
    url: "your_url", 
    type: "GET", 
    dataType: "json", 
    timeout: 5000, /* timeout in milliseconds */ 
    success: function(response) { alert(response); }, 
    error: function(jqXHR, exception) { 
     if (jqXHR.status === 0) { 
      return ('Not connected.\nPlease verify your network connection.'); 
     } else if (jqXHR.status == 404) { 
      return ('The requested page not found. [404]'); 
     } else if (jqXHR.status == 500) { 
      return ('Internal Server Error [500].'); 
     } else if (exception === 'parsererror') { 
      return ('Requested JSON parse failed.'); 
     } else if (exception === 'timeout') { 
      return ('Time out error.'); 
     } else if (exception === 'abort') { 
      return ('Ajax request aborted.'); 
     } else { 
      return ('Uncaught Error.\n' + jqXHR.responseText); 
     } 
    } 
});​