簡體   English   中英

Ajax表單超時

[英]Ajax form timing out

我有一個PHP頁面,表單中包含100多個字段。 我正在使用Ajax鍵入數據將內容提取到每個字段。

有時候,碰巧我得到這個錯誤:

凈:: ERR_CONNECTION_TIMED_OUT

大概是因為服務器無法及時響應。 發生這種情況時,將出現錯誤,並且Ajax將無法繼續工作。

有沒有辦法可以使代碼即使在發生錯誤后也能執行? 這樣我才能繼續嘗試Ajax?

注意 :我使用的是普通的舊XMLHttpRequest()請求,而不是jquery函數

在您的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
    }

希望會有所幫助

您可以使用此函數捕獲可以在ajax中獲得的所有類型的錯誤:

$.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);
        }
    }
});​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM