handle ajax error when a user clicks refresh

后端 未结 5 1170
忘掉有多难
忘掉有多难 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:28

    [this is an edit from the previous answer, which had outstanding questions that I have since resolved]

    jQuery throws the error event when the user navigates away from the page either by refreshing, clicking a link, or changing the URL in the browser. You can detect these types of errors by by implementing an error handler for the ajax call, and inspecting the xmlHttpRequest object:

    $.ajax({
        /* ajax options omitted */
        error: function (xmlHttpRequest, textStatus, errorThrown) {
             if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) 
                  return;  // it's not really an error
             else
                  // Do normal error handling
    });
    

提交回复
热议问题