Skip to content

Instantly share code, notes, and snippets.

@higeorange
Created February 1, 2009 10:48
Show Gist options
  • Save higeorange/55847 to your computer and use it in GitHub Desktop.
Save higeorange/55847 to your computer and use it in GitHub Desktop.
jQuery AjaxPost
(function($) {
$.fn.ajaxPost = function(callback, options) {
var default_options = {
data_type: 'html',
before_send: function() {},
error_handler: function() {}
};
options = $.extend(default_options, options)
return this.each(function(){
if(this.tagName.toLowerCase() != 'form') return;
var f = $(this);
var submit = f.find('button[@type="submit"], input[@type="submit"]')
f.submit(function(e) {
e.preventDefault();
$.ajax({
url: f.attr('action'),
type: 'POST',
data: f.serialize() + '&mode=ajax',
dataType: options.data_type,
beforeSend: function() {
submit.attr('disabled', 'disabled')
options.before_send();
},
success: callback,
error: options.error_handler,
complete: function(xhr, textStatus) {
submit.removeAttr('disabled')
}
});
});
});
}
})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment