Javascript 将事件绑定到jQuery.post和回调

Javascript 将事件绑定到jQuery.post和回调,javascript,jquery,events,Javascript,Jquery,Events,我在项目中经常使用jQuery.post,每次浏览器发送post请求时,我都希望在请求处理过程中显示预加载程序,然后在收到服务器的回复时停止预加载程序: var params = {'action':'get_customer_info', 'customerID':4}; preloader.start(); $.post('ajax/url', params, function(response){ preloader.stop(); responseHandler(re

我在项目中经常使用jQuery.post,每次浏览器发送post请求时,我都希望在请求处理过程中显示预加载程序,然后在收到服务器的回复时停止预加载程序:

var params = {'action':'get_customer_info', 'customerID':4};

preloader.start();
$.post('ajax/url', params, function(response){
    preloader.stop();

    responseHandler(response);
});
与每次调用jQuery的post时都添加preload.start()和preload.stop()行不同,我希望在jQuery.post之前以及成功/失败处理程序上绑定/触发事件

一般来说,我知道如何绑定和触发事件,但我不确定如何使用$.post和处理程序


如何实现这一点?

您可以设置全局ajax事件,这样预加载程序就会在每个ajax请求上显示

$(document).ajaxSend(function() {
    preloader.start();
}).ajaxComplete(function() {
    preloader.stop();
});
或者您可以创建自己的
post
函数

function post(url, params) {
    preloader.start();
    return $.post('ajax/url', params, function(response){
        preloader.stop();
    });
}

post('ajax/url', params).done(function(response) {
    responseHandler(response);
});

您可以设置全局ajax事件,这样预加载程序就会在每个ajax请求上显示

$(document).ajaxSend(function() {
    preloader.start();
}).ajaxComplete(function() {
    preloader.stop();
});
或者您可以创建自己的
post
函数

function post(url, params) {
    preloader.start();
    return $.post('ajax/url', params, function(response){
        preloader.stop();
    });
}

post('ajax/url', params).done(function(response) {
    responseHandler(response);
});