Jquery Ajax error handling to ignore aborted

Error comes from when the web page refresh too much fast than it can load.

For example, a web page load a huge data, even with ajax pagination, and users refresh multiple times of this page

 

Solution which resolved this problem :

var unloaded = false;
$(window).bind('beforeunload', function(){
    unloaded = true;
});

$(document).ajaxError(function(event, request, settings) {
    if (unloaded || request.statusText == "abort") {
        return;
    }
    ...
}

Ref.
http://stackoverflow.com/questions/4807572/jquery-ajax-error-handling-to-ignore-aborted

Advertisements

[jQuery] Comparison of getJSON and ajax

Comparison of jQuery getJSON and ajax.

I tried to debug this code in jQuery(the following), the object is to get the data from a json file and re-format the output data into an array, this array will be used in others functions later.

$(document).ready(function() {
  var arr = [];
  // Get data from a json file
  $.getJSON("jsonfile.json", {}, function(data) {
    $.each(data, function(key, value) {
      arr.push(value); // Add data value to an array
    });
  });
  console.log(arr); // Print the array to console
});

Continue reading