// Create an object to handle our AJAX. function AJAX(){ var objSelf = this; // This struct will cache the current XmlHTTP requests // so that we can reference them if a call fails. this.CurrentRequests = {}; } // This handles the JSON request. This checks to see if the current // request is already being processed and also handles any error // wiring that is required. AJAX.prototype.GetJSON = function( $1, $2, $3, $4 ){ var objSelf = this; var strName = $1; var strURL = $2; var objOptions = $3; var fnCallback = $4; // Check to see if there are only three arguments. If there // are only 3, then the first one (name of request) which is // optional was not passed in. Shift the other arguments // to the appropriate variables. if (arguments.length == 3){ // Name is not being used. strName = null; strURL = $1; objOptions = $2; fnCallback = $3; } // First, we have to check to see if this request is // already being processed. We don't want the user to // try and fire off multiple requests of the same type. // Of course, if the name is NULL, then don't worry. if (!strName || !this.CurrentRequests[ strName ]){ // Store current request. this.CurrentRequests[ strName ] = true; // Make actual AJAX request. $.ajax( { // Basic JSON properties. url: strURL, data: objOptions, dataType: "json", // The success call back. success: function( objResponse ){ // Remove request flag. objSelf.CurrentRequests[ strName ] = false; // Pass off to success handler. fnCallback( objResponse ); }, // The error handler. error: function( objRequest ){ // Remove request flag. objSelf.CurrentRequests[ strName ] = false; // Pass off to fail handler. objSelf.AJAXFailHandler( objRequest, fnCallback ); } } ); } else { // This request is currently being processed. alert( "Request being processed. Be patient." ); } } // This will handle all AJAX failures. AJAX.prototype.AJAXFailHandler = function( objRequest, fnCallback ){ // Since this AJAX request failed, let's call the callback // but manually create a failure response. fnCallback( { SUCCESS: false, DATA: "", ERRORS: [ "Request failed" ] } ); }