Skip to content

Instantly share code, notes, and snippets.

@ka8725
Created December 27, 2011 21:20
Show Gist options
  • Save ka8725/1525200 to your computer and use it in GitHub Desktop.
Save ka8725/1525200 to your computer and use it in GitHub Desktop.
$.fn.submitWithAjax = function(params) {
var options = {
type : this.attr('method') || 'POST',
url : this.attr("action"),
data : this.serialize(),
callbacks : {},
target : '#temp',
freezing : this,
dataType : 'script',
autoActivate : true
};
$.extend(options, params);
var loader_id = $.AjaxLoader.on(options.freezing);
$.ajax({
type: options.type,
url: options.url,
data: options.data,
success: function(data, textStatus, XMLHttpRequest) {
if (options.autoActivate || (data.code != 0)) { $.AjaxLoader.off(loader_id, 0); }
if (options.callbacks.success) { options.callbacks.success(data, textStatus, XMLHttpRequest); }
},
error: function(jqXHR, textStatus, errorThrown) {
if (options.callbacks.error) {
$.AjaxLoader.off(loader_id);
options.callbacks.error(jqXHR, textStatus, errorThrown);
} else {
$.AjaxLoader.off(loader_id,1);
}
},
dataType: options.dataType
});
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment