handle ajax error when a user clicks refresh

后端 未结 5 1173
忘掉有多难
忘掉有多难 2020-12-02 17:17

i apologise if this is something i should be able to look up. all of the terms i wanted were way overloaded..

here is my problem: when i open a page, it fires off a

5条回答
  •  渐次进展
    2020-12-02 17:47

    var isPageBeingRefreshed = false;
    
    window.onbeforeunload = function() {
        isPageBeingRefreshed = true;
    };
    
    $.ajax({
        error: function (xhr, type, errorThrown) {
            if (!xhr.getAllResponseHeaders()) {
                xhr.abort();
                if (isPageBeingRefreshed) {
                    return; // not an error
                }
            }
        }
    });
    

提交回复
热议问题