封装了一个ajax方法,想把回调函数作为封装好的参数传入,现在的做法是function aa(m){
m();
}function bb(){
alert(1);
}
aa(bb);能运行,但总感觉不对,上网查了查,有这么做的,不过也有说是错的,所以来问问各位大神,小弟拜谢!
1 回答
森林海
TA贡献2011条经验 获得超2个赞
代码如下,是这种逻辑
fn
function ajax(args) {
$.ajax({ type: args.type || 'post',
url: args.url,
data: args.data,
async: args.async || true,
dataType: args.datatype || 'json',
success: function (m, status, xhr) {
args.success(m);
}, error: function(xhr, status, error) {
console.log('xhr.status: ' + xhr.status + ' status: ' + status);
}
});
}调用
ajax({ url: 'http://', data: {}, success: function(j) {
console.log(j); //显示ajax返回的值
}
});添加回答
举报
0/150
提交
取消
