Skip to content

Instantly share code, notes, and snippets.

@bjcull
Created August 18, 2010 12:25
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bjcull/534526 to your computer and use it in GitHub Desktop.
Save bjcull/534526 to your computer and use it in GitHub Desktop.
// jquery.jqax.js - A plugin for jQuery ajax wrapping some common
// functionality aimed at .NET services and page methods
// Ben Cull - 18 August 2010
(function($) {
$.jQax = function(options) {
// Load Defaults and make them public
this.defaults = {
ShowLoading: true,
LoaderContainerId: "#divLoader",
LoaderText: "Loading...",
ErrorCallBack: function(XMLHttpRequest, textStatus, errorThrown) {
var response = eval("(" + XMLHttpRequest.responseText + ")");
var output = "Message: " + response.Message + "\n\n";
output += "StackTrace: " + response.StackTrace;
alert(output);
}
};
// Extend defaults into options
var o = $.extend({}, this.defaults, options);
this.Get = function(WebService, GetData, SuccessCallBack, ShowLoading, LoaderText, ErrorCallback) {
if (ShowLoading != null ? ShowLoading : o.ShowLoading)
ShowLoader(LoaderText != null ? LoaderText : o.LoaderText);
if (WebService == "")
return 1; // Incorrect Parameters
if (ErrorCallback == null)
ErrorCallback = o.ErrorCallBack;
if (GetData != null && typeof (GetData) === "object")
GetData = $.param(GetData);
$.ajax({
type: "GET",
url: WebService,
data: GetData,
success: SuccessCallBack,
error: ErrorCallback
});
return 0; // Successful
};
this.Post = function(WebService, JsonData, SuccessCallBack, ShowLoading, LoaderText, ErrorCallback) {
if (ShowLoading != null ? ShowLoading : o.ShowLoading)
ShowLoader(LoaderText != null ? LoaderText : o.LoaderText);
if (WebService == "")
return 1; // Incorrect Parameters
if (ErrorCallback == null)
ErrorCallback = o.ErrorCallBack;
if (typeof (JsonData) === "object")
JsonData = JSON.stringify(JsonData);
$.ajax({
type: "POST",
url: WebService,
data: JsonData,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: SuccessCallBack,
error: ErrorCallback
});
return 0; // Successful
};
function ShowLoader(LoaderText) {
$(o.LoaderContainerId).text(LoaderText);
$(o.LoaderContainerId).fadeIn("fast");
};
$(document).ready(function() {
$(o.LoaderContainerId).ajaxStop(function() {
$(this).stop(true, true).fadeOut("fast");
});
});
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment