AJAX will end up hitting error method when any event in page cause page load or page unload.
OCCURS:
Why this occurs is due to the ajax call in the middle of fetching data from server and the page is forcing it to close connection
SOLUTION:
SOLUTION:
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:
- if (xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0)
- return true; // it's not really an error
- else {
- alert('[functionname]::' + xhr.responseText + '::[thrownerror]::' + thrownError);
- OnError(xhr, thrownError);
- }
- ///ERROR of ajax call
- error: function (xmlHttpRequest, textStatus, errorThrown) {
- if (xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0)
- return true; // it's not really an error
- else {
- alert('[functionname]::' + xhr.responseText + '::[thrownerror]::' + thrownError);
- OnError(xhr, thrownError);
- }
- }
Alternative Approach:
you can check it by windows.unload event but its a function call more but more reliable
- var unloaded = false;
- $.ajax(...) ...
- .error(function(jqXHR) {
- if (unloaded) return; // Ignore errors caused by navigating away
- // Now, check for real errors ..
- });
- $(window).unload(function() {unloaded = true;});
0 comments:
Post a Comment