O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required