Getting the progress of a request using jQuery

jQuery abstracts the various progress reporting mechanisms of the underlying XMLHttpRequest object in a platform-agnostic way, giving you the ability to determine whether your request succeeded or failed. You do this by registering functions that the jQuery AJAX handler will invoke when an error occurs or the results are successfully loaded.

How to do it...

Here's doAjax rewritten to support getting notifications on failure, regardless of whether the event succeeds or fails:

function doAjax() {
  $('#debug').html("loaded... executing.");
 
  var request = { 
    call: "kf6gpe-7"
  };

  $.ajax({
    type: "POST",
    url: "/",
    data: JSON.stringify(request),
    dataType:"json",
  })
  .fail(function() {
 $('#debug').append("<br/>failed"); ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.