php_nub_qq
php_nub_qq

Reputation: 16015

Jquery ajax default success function with custom function

What I wanna do is have a default success function which all requests will go through and then a specific function with stuff for individual calls.

Something like

function handleResponse(data, func){
    if(data.error){
        display error information
    }else{
        func(data)
    }
}

$.ajax({
    success: handleResponse(data, function(){
        Individual request callback function
    })
});

Not quite sure if that would work by itself but I am writing this so you can understand the logic of what I'm aiming to do. What I want to ask is if there is a way I could pass this handleResponse function in $.ajaxSetup or something so I can just handle ajax response like I would normally with just a single anonymous function and have handleResponse be called automatically?

Thanks!

Upvotes: 4

Views: 3107

Answers (7)

REMITH
REMITH

Reputation: 1107

Its the simple way to do common ajax validations.

$.ajax({
   success: function(data){
                alert("Hi");
//DO WHATEVER VALIDATIONS YOU WANT TO DO HERE
                ajaxPostSuccess(data);
            }
});

function ajaxPostSuccess(data){
//DO THE REMAINING HERE
}

Upvotes: 0

Alnitak
Alnitak

Reputation: 339816

IMHO, the best way to do this is to use "deferred" objects, along with a function that will pre-process your AJAX results before passing them to your own callbacks:

function errorCheck(data) {
    if (data.error) {
        // display errors
        ...
        return $.Deferred().reject();  // "fake" a failure
    } else {
        return arguments;
    }
}

with usage:

$.ajax(...).then(errorCheck).done(function(data) {
    // normal callback function processing
    ...
}).fail(function() {
    // called if AJAX fails, or if data.error is set
    ...
});

This allows you to decide on a per-call basis whether you want to use the errorCheck logic or not, and also allows use of errorCheck with $.get, $.post etc.

Upvotes: 2

Salketer
Salketer

Reputation: 15711

$(document).ajaxcomplete(function(){
//This function is executed on every ajax call completion.
});

Then you simply do your request as you would...

$.ajax({
    url: "/path/",
    success: function(){
        //My handler for this call.
    }
});

You can read more at http://api.jquery.com/category/ajax/global-ajax-event-handlers/

Upvotes: 0

Jim Jeffries
Jim Jeffries

Reputation: 10081

I don't think anything like this exists already, but you could qrap the ajax call in something like

function doAjax(url, callback) {
  $.ajax({
    url: "/path/",
    success: function (...) {
      handleResponse();
      callback();
    }
  });
}

doAjax("/path", function () { .. } );

Upvotes: 0

Dias
Dias

Reputation: 421

Why not ?

$.ajax({
    url: "/path/",
    success: function(data){
       handleResponse(data, function(){
       });
    }
});

function handleResponse(data, callback){
   if(data.error){
      display error information
   }else{
      callback.call();
   }
}

Upvotes: 0

nnnnnn
nnnnnn

Reputation: 150030

When jQuery calls your success handler it isn't expecting to pass it a function like that, but you can do something like the following:

function makeResponseHandler(func){
    return function(data) {
       if(data.error){
          // display error information
       }else{
          func(data);
       }
    };
}

$.ajax({
    success: makeResponseHandler(function(){
        //Individual request callback function
    })
});

The line with success: ... calls the makeResponseHandler() function immediately, passing it your individual anonymous function. The makeResponseHandler() function then returns another function and it is that function that becomes the success callback.

Upvotes: 3

suhailvs
suhailvs

Reputation: 21680

try

$.ajax({
    url: "/path/",
    success: handleResponse
});

Upvotes: 0

Related Questions