Simple jQuery Ajax Handler
Ajax (Asynchronous XML and HTTP) request is necessary thing now a days in every web application. jQuery library provides really good wrapper against plain Javascript XMLHttpRequest. While writing real-time web applications we need to repeat same code again and again. To reduce the code, I wrote a simple wrapper function against jQuery ajax() method.
/** * This is generic ajax request handler to handle all ajax request * * @param {Object} callUrl : This is url of function to be called * @param {Object} postData : This is data to be post or get * @param {Object} method : post or get method * @param {Object} asyncStatus : async status TRUE or FALSE * @param {Object} successCallback : success state handler */ var ajaxHandler = function(callUrl, postData, method, asyncStatus, successCallback) { jQuery.ajax({ url : callUrl, data : JSON.stringify(postData), type : method, async : asyncStatus }) .done(successCallback) .error(function(xhr, textStatus, errorThrown) { if(xhr.status = 400){ console.log('Method Not Found'); } }); };
The ajaxHandler code is also hosted on Github. Please take a look at the Ajax Handler.