Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:46:21+00:00 2026-05-28T01:46:21+00:00

I have been working with jQuery and AJAX for a few weeks now and

  • 0

I have been working with jQuery and AJAX for a few weeks now and I saw two different ways to ‘continue’ the script once the call has been made: success: and .done.

From the synopsis from the jQuery documentation we get:

.done(): Description: Add handlers to be called when the Deferred object is resolved.

success: (.ajax() option): A function to be called if the request succeeds.

So, both do something after the AJAX call has been completed/resolved. Can I use one or the other randomly? What is the difference and when one is used instead of the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:46:22+00:00Added an answer on May 28, 2026 at 1:46 am

    success has been the traditional name of the success callback in jQuery, defined as an option in the ajax call. However, since the implementation of $.Deferreds and more sophisticated callbacks, done is the preferred way to implement success callbacks, as it can be called on any deferred.

    For example, success:

    $.ajax({
      url: '/',
      success: function(data) {}
    });
    

    For example, done:

    $.ajax({url: '/'}).done(function(data) {});
    

    The nice thing about done is that the return value of $.ajax is now a deferred promise that can be bound to anywhere else in your application. So let’s say you want to make this ajax call from a few different places. Rather than passing in your success function as an option to the function that makes this ajax call, you can just have the function return $.ajax itself and bind your callbacks with done, fail, then, or whatever. Note that always is a callback that will run whether the request succeeds or fails. done will only be triggered on success.

    For example:

    function xhr_get(url) {
    
      return $.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        beforeSend: showLoadingImgFn
      })
      .always(function() {
        // remove loading image maybe
      })
      .fail(function() {
        // handle request failures
      });
    
    }
    
    xhr_get('/index').done(function(data) {
      // do stuff with index data
    });
    
    xhr_get('/id').done(function(data) {
      // do stuff with id data
    });
    

    An important benefit of this in terms of maintainability is that you’ve wrapped your ajax mechanism in an application-specific function. If you decide you need your $.ajax call to operate differently in the future, or you use a different ajax method, or you move away from jQuery, you only have to change the xhr_get definition (being sure to return a promise or at least a done method, in the case of the example above). All the other references throughout the app can remain the same.

    There are many more (much cooler) things you can do with $.Deferred, one of which is to use pipe to trigger a failure on an error reported by the server, even when the $.ajax request itself succeeds. For example:

    function xhr_get(url) {
    
      return $.ajax({
        url: url,
        type: 'get',
        dataType: 'json'
      })
      .pipe(function(data) {
        return data.responseCode != 200 ?
          $.Deferred().reject( data ) :
          data;
      })
      .fail(function(data) {
        if ( data.responseCode )
          console.log( data.responseCode );
      });
    }
    
    xhr_get('/index').done(function(data) {
      // will not run if json returned from ajax has responseCode other than 200
    });
    

    Read more about $.Deferred here: http://api.jquery.com/category/deferred-object/

    NOTE: As of jQuery 1.8, pipe has been deprecated in favor of using then in exactly the same way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working in Sencha Touch for a few weeks now, and while
I am very new to javascript and ajax/jquery and have been working on trying
I have some Ajax that has been working on a live site, now it
I have been working on a JQuery script that posts JSON to my asp.net
I've been working on updating my page using javascript/jquery and have now run into
I have been working with AJAX for a while now, but in limited and
I have been working with jQuery mobile 1.0.1. I have a page which drills
Alright have been working to switch to jQuery 1.7's new and improved .on() function
I'm working on a asp.net mvc2 app. I have been using jquery to do
I have been working with android for a little while now and feel pretty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.