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

  • SEARCH
  • Home
  • 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 3998244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:30:06+00:00 2026-05-20T07:30:06+00:00

I am creating a ajax utility for interfacing with my server methods. I would

  • 0

I am creating a ajax utility for interfacing with my server methods. I would like to leverage jQuery 1.5+ deferred methods from the object returned from the jQuery.ajax() call. The situation is following.

  1. The serverside method always returns a JSON object:

    { success: true|false, data: ... }

  2. The client-side utility initiates the ajax call like this

    var jqxhr = $.ajax({ ... });

  3. And the problem area:

    jqxhr.success(function(data, textStatus, xhr) {
         if(!data || !data.success) { 
             ???? // abort processing, trigger error
         }
    });
    return jqxhr; // return to caller so he can attach his own handlers
    

So the question is how to cancel invocation of all the callers appended success callbacks an trigger his error handler in the place mentioned with ???? ?

The documentation says the deferred function invocation lists are FIFO, so my success handler is definitely the first one.

  • 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-20T07:30:07+00:00Added an answer on May 20, 2026 at 7:30 am

    (UPDATE:
    Please note that currently jQuery Promises are not compatible with the Promises/A+ specification – more info in this answer.)

    In your function where you create the AJAX request you can also create a deferred object and return a promise to the caller after binding its resolve and reject functions to the appropriate callbacks of the $.ajax request with some custom data verification, like this:

    function makerequest() {
    
        var deferred = $.Deferred();
        var promise = deferred.promise();
    
        var jqxhr = $.ajax({
            // ...
        });
    
        jqxhr.success(function(data, status, xhr) {
            if (!data || !data.success) {
                deferred.reject(jqxhr, 'error');
            } else {
                deferred.resolve(data, status, xhr);
            }
        });
    
        jqxhr.error(function(jqXHR, status, error) {
            deferred.reject(jqXHR, status, error);
        });
    
        return promise;
    }
    

    Now anyone will be able to use it like any promise like this to your function:

    var request = makerequest();
    
    request.done(successCallback);
    request.fail(errorCallback);
    

    Or even just:

    makerequest().then(successCallback, errorCallback);
    

    If you also add this:

        promise.success = promise.done;
        promise.error = promise.fail;
    

    then your caller will have (maybe more familiar) interface of .success() and .error() like with pure $.ajax() calls:

    var request = makerequest();
    
    request.success(successCallback);
    request.error(errorCallback);
    

    (The implementation of .complete() is left as an exercise for the reader.)

    See this demos:

    • http://jsfiddle.net/_rsp/r2YnM/
    • http://jsfiddle.net/_rsp/r2YnM/1/ (more verbose)

    Here’s another example pulled directly from a working project:

    function ajax(url, data) {
        var self = this;
        var deferred = $.Deferred();
        var promise = deferred.promise();
    
        var jqxhr = $.ajax({
            url: url,
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            type: 'POST'
        }).done(function (msg, status, xhr) {
            if (!msg || msg.Error) {
                self.doError(msg.Error);
                deferred.reject(jqxhr, 'error');
            } else {
                deferred.resolve(msg, status, xhr);
            }
        });
    
        return promise;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an Ajax autocomplete application and would like know if there is
I have a div which is creating through ajax, i would like to disable
I am just getting started creating an AJAX application using server side push. I
I'm creating a simple ajax call to retrieve a list of user reviews. I
I am creating a custom ASP.NET AJAX server control in which multiple instances of
I'm creating a CMS using jQuery and AJAX. When I click, my Add Campaign
I am creating a web app which uses jQuery to authenticate: $.ajax({ url: /session/create?format=json,
I have jQuery.ajax() creating a request to a url (cms2/docman/dir/%id) (%id is a numeric
I am creating a loginform like this: @using (Ajax.BeginForm(Login, new AjaxOptions() { HttpMethod =
Im creating a GSP form that i wish to submit using the $.ajax() call.

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.