简体   繁体   English

jQuery AJAX自定义函数和自定义回调?

[英]jQuery AJAX custom function and custom callback?

Heylow everyone! 大家好!

I have an ajax() call like so: 我有一个像这样的ajax()调用:

$.ajax({
  type: "post",
  url: "whatever.php",
  data: {
    theData: "moo moo"
  },
  success: function(data) {
    console.log(data);
  }
});

Is it possible to wrap this inside a custom function but retain the callback? 是否可以将其包装在自定义函数中保留回调?
Something like: 就像是:

function customAjax(u, d, theCallbackStuff) {
  $.ajax({
    type: "post",
    url: u,
    data: d,
    success: function(data) {
      //RUN theCallbackStuff
    }
  });
}

theCallbackStuff will be something like: theCallbackStuff将是这样的:

var m = 1;
var n = 2;
alert(m + n + data);

EDIT: 编辑:

Got a recent upvote for this and I feel compelled to state that I would no longer do it this way. 最近得到了一个upvote,我觉得有必要声明我不再这样做了。 $.ajax returns a promise so you can do pretty much what i just did here in a more consistent and robust way using the promise directly. $.ajax返回一个promise所以你可以直接使用promise,以更加一致和健壮的方式完成我刚才所做的事情。

function customRequest(u,d) {
   var promise = $.ajax({
     type: 'post',
     data: d,
     url: u
   })
   .done(function (responseData, status, xhr) {
       // preconfigured logic for success
   })
   .fail(function (xhr, status, err) {
      //predetermined logic for unsuccessful request
   });

   return promise;
}

Then usage looks like: 用法如下:

// using `done` which will add the callback to the stack 
// to be run when the promise is resolved
customRequest('whatever.php', {'somekey': 'somevalue'}).done(function (data) {
   var n = 1,
       m = 2;

   alert(m + n + data);
});

// using fail which will add the callback to the stack 
// to be run when the promise is rejected
customRequest('whatever.php', {'somekey': 'somevalue'}).fail(function (xhr, status, err) {
   console.log(status, err);
});

// using then which will add callabcks to the 
// success AND failure stacks respectively when 
// the request is resolved/rejected
customRequest('whatever.php', {'somekey': 'somevalue'}).then(
  function (data) {
     var n = 1,
         m = 2;

     alert(m + n + data);
  },
  function (xhr, status, err) {
     console.log(status, err);
  });

Sure i do this all the time. 当然,我一直这样做。 You can either execute the callback within the actual success callack or you can assign the callback as the success callback: 您可以在实际成功的callack中执行回调,也可以将回调分配为成功回调:

function customRequest(u,d,callback) {
   $.ajax({
     type: "post",
     url: u,
     data:d,
     success: function(data) {
        console.log(data); // predefined logic if any

        if(typeof callback == 'function') {
           callback(data);
        }
     }
  });
}

Usage would look something like: 用法看起来像:

customRequest('whatever.php', {'somekey': 'somevalue'}, function (data) {
   var n = 1,
       m = 2;

   alert(m + n + data);
});
    function customAjax(u, d, theCallbackStuff) {
      $.ajax({
        type: "post",
        url: u,
        data: d,
        success: theCallbackStuff 
      });
    }

customAjax(url, data, function(data){
//do something
});

On this note, you can pass a complete function as a callback to this: 在此注释中,您可以将完整函数作为回调传递给此:

function customRequest(u,d,callback) {
   $.ajax({
     type: "post",
     url: u,
     data:d,
     success: function(data) {
        console.log(data); // predefined logic if any

        if(typeof callback == 'function') {
           callback(data);
        }
     }
  });
}



// Then call it as follows:

function initiator() {

    customRequest( '/url/to/post', 'param1=val', function() { alert( 'complete' ); })

}

Simply passing it as an anonymous function will work too.. Just for the sake of showing :) 简单地将它作为匿名函数传递也会起作用..只是为了显示:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM