Jquery ajax默认成功函数与自定义函数

时间:2022-10-23 20:38:47

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?

不太确定这是否可以单独使用,但我写这篇文章是为了让你能理解我的目标。我想问的是,是否有一种方法可以在$ .ajaxSetup中传递这个handleResponse函数或者其他什么,所以我可以像通常只使用一个匿名函数一样处理ajax响应并自动调用handleResponse?

Thanks!

谢谢!

6 个解决方案

#1


3  

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:

当jQuery调用你的成功处理程序时,它不希望传递类似这样的函数,但你可以执行以下操作:

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.

成功的一行:...立即调用makeResponseHandler()函数,传递你的个人匿名函数。然后makeResponseHandler()函数返回另一个函数,该函数成为成功回调函数。

#2


2  

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:

恕我直言,这样做的最好方法是使用“延迟”对象,以及在将AJAX结果传递给您自己的回调之前预先处理您的AJAX结果的函数:

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.

这允许您根据每个调用决定是否要使用errorCheck逻辑,还允许使用带有$ .get,$ .post等的errorCheck。

#3


0  

try

尝试

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

#4


0  

Why not ?

为什么不 ?

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

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

#5


0  

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

我不认为这样的事情已经存在了,但你可以用类似的东西来调整ajax调用

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

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

doAjax(“/ path”,function(){..});

#6


0  

$(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/

您可以在http://api.jquery.com/category/ajax/global-ajax-event-handlers/阅读更多信息。

#1


3  

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:

当jQuery调用你的成功处理程序时,它不希望传递类似这样的函数,但你可以执行以下操作:

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.

成功的一行:...立即调用makeResponseHandler()函数,传递你的个人匿名函数。然后makeResponseHandler()函数返回另一个函数,该函数成为成功回调函数。

#2


2  

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:

恕我直言,这样做的最好方法是使用“延迟”对象,以及在将AJAX结果传递给您自己的回调之前预先处理您的AJAX结果的函数:

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.

这允许您根据每个调用决定是否要使用errorCheck逻辑,还允许使用带有$ .get,$ .post等的errorCheck。

#3


0  

try

尝试

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

#4


0  

Why not ?

为什么不 ?

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

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

#5


0  

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

我不认为这样的事情已经存在了,但你可以用类似的东西来调整ajax调用

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

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

doAjax(“/ path”,function(){..});

#6


0  

$(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/

您可以在http://api.jquery.com/category/ajax/global-ajax-event-handlers/阅读更多信息。