Heylow大家!
我有一个阿贾克斯()
调用像这样:
$。阿贾克斯({
类型:后,
网址:whatever.php
数据: {
海图:哞哞
},
成功:功能(数据){
的console.log(数据);
}
});
是否可以自定义函数内包装这个但是保留回调? 是这样的:
函数customAjax(U,D,theCallbackStuff){
$阿贾克斯({
类型:后,
网址:U,
数据:D,
成功:功能(数据){
//运行theCallbackStuff
}
});
}
theCallbackStuff
将是这样的:
VAR M = 1;
变种N = 2;
警报(M + N +数据);
解决方案
编辑:的
有一个最近的upvote这个,我觉得有必要指出,我会不孤独的人,以这种方式。 $。阿贾克斯
返回诺
所以你可以做pretty的太多我只是做了一个比较一致的位置并直接使用的承诺可靠的方法。
函数customRequest(U,D){
VAR承诺= $阿贾克斯({
类型:'后',
数据:D,
网址:U
})
.done(功能(responseData,状态XHR){
成功// preconfigured逻辑
})
.fail(功能(XHR,状态,ERR){
不成功的请求// predetermined逻辑
});
返回的承诺;
}
然后用法如下:
//使用`done`将回调添加到堆栈
当承诺解决//要运行
customRequest('whatever.php',{'somekey:someValue中'})。完成(功能(数据){
变种N = 1,
米= 2;
警报(M + N +数据);
});
//使用失败,这将回调添加到堆栈
当承诺被拒绝//要运行
customRequest('whatever.php',{'somekey:someValue中'})。完成(功能(XHR,状态,ERR){
的console.log(状态,ERR);
});
//使用那么这将增加callabcks到
//成功和失败分别叠加时,
//请求解析/拒绝
customRequest('whatever.php',{'somekey':'someValue中'})。然后(
功能(数据){
变种N = 1,
米= 2;
警报(M + N +数据);
},
功能(XHR,状态,ERR){
的console.log(状态,ERR);
});
当然我做这一切的时候。您可以在实际成功callack内执行的回调,也可以指定回调的成功回调:
函数customRequest(U,D,回调){
$阿贾克斯({
类型:后,
网址:U,
数据:D,
成功:功能(数据){
的console.log(数据); // predefined逻辑,如果有的话
如果(typeof运算回调=='功能'){
回调(数据);
}
}
});
}
使用情况看起来是这样的:
customRequest(whatever.php',{'somekey:someValue中'},功能(数据){
变种N = 1,
米= 2;
警报(M + N +数据);
});
Heylow everyone!
I have an ajax()
call like so:
$.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:
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 loner to it this way. $.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.
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'}).done(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:
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);
});