网络连接丢失时处理Ajax

时间:2018-06-04 05:19:31

标签: javascript ajax

如何在网络连接丢失时处理Ajax。 Ajax不返回任何状态并保持“(挂起)”状态。

我用$.ajax()方法

尝试了这个
.error: function(err){
console.log(err)
}

并在错误时尝试了以下内容:

 function makeXMLHttpRequest(url, data, callback) {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
            console.log("makeXMLHttpRequest:",request)
            if (request.readyState == 4 && request.status == 200) {
                //clearTimeout(xhrTimeout);   // Timeout when the  upload complete

            }
            if(request.status >= 400 && request.status <= 511)
            {
                var error = new Error(request.responseText)
                error.code = xhr.status

                request.abort();
            }
        };

request.onerror = function(XMLHttpRequest, textStatus, errorThrown) {

            if (XMLHttpRequest.readyState == 4) {
                // HTTP error (can be checked by XMLHttpRequest.status and XMLHttpRequest.statusText)

            }
            else if (XMLHttpRequest.readyState == 0) {
                // Network error (i.e. connection refused, access denied due to CORS, etc.)
            }
            else {
                // something weird is happening

            }

        }

任何人都知道如何在'网络连接丢失'上处理ajax?

请注意:网络连接丢失与超时不同。在超时时,我们在ajax中有可用的事件,但我不是在寻找它。

3 个答案:

答案 0 :(得分:1)

function makeXMLHttpRequest(url, data, callback) {
        var request = new XMLHttpRequest();
        request.timeout = 3000; // in milli seconds, so 3 seconds to wait before it timesout
        request.onreadystatechange = function() {
            console.log("makeXMLHttpRequest:",request)
            if (request.readyState == 4 && request.status == 200) {
                //clearTimeout(xhrTimeout);   // Timeout when the  upload complete

            }
            if(request.status >= 400 && request.status <= 511)
            {
                var error = new Error(request.responseText)
                error.code = xhr.status

                request.abort();
            }
        };
request.ontimeout = function(error) {
    //handle the timeout
};
request.onerror = function(XMLHttpRequest, textStatus, errorThrown) {

            if (XMLHttpRequest.readyState == 4) {
                // HTTP error (can be checked by XMLHttpRequest.status and XMLHttpRequest.statusText)

            }
            else if (XMLHttpRequest.readyState == 0) {
                // Network error (i.e. connection refused, access denied due to CORS, etc.)
            }
            else {
                // something weird is happening

            }

        }

Refer the MDN XMLHttpRequest timeout link

答案 1 :(得分:0)

您可以在AJAX调用中添加超时。这样,如果你在X秒内没有得到响应,你的.success对象将返回状态为“timeout”,你可以抛出错误。例如:

$.ajax({
url: url,
data: data,
timeout: 5000,
success: function(data){
    if(data.status == "timeout"){
        alert("Internet connection problem!");
    }
}
});

答案 2 :(得分:0)

您可以使用offline事件

全局设置离线应用程序行为
window.addEventListener('offline', function(e) { 
  console.log('offline');
});