Reusable jquery ajax requests?

As all your functions have similar AJAX setup options I would recommend you to setup common options globally using the $. AjaxSetup function.

As all your functions have similar AJAX setup options I would recommend you to setup common options globally using the $. AjaxSetup function: $(function() { $. AjaxSetup({ contentType: 'application/json; charset=utf-8', global: false, async: false, dataType: 'json', beforeSend: function() { $('.

Loading').show(); }, complete: function() { $('. Loading').hide(); }, }); }); Next let's first consider the getClients and getDrivers functions. Those look pretty much the same.

The only difference I can see between them is the url you are sending the AJAX request and the html that's being appended to the #ResultsDiv. So you could have two separate functions that handle the result: function formatDriversResult(result) { return '' + result. DriverName + 'Mobile No : ' + result.

DriverMobileNo + ''; } and function formatClientsResult(result) { return '' + result. ClientName + 'Mobile No : ' + result. ClientMobNo + 'Address : ' + result.

ClientAddress + ''; } And finally the two functions could be merged into a single one: function getEntities(url, currentPage, formatResultFunction) { $. Ajax({ url: url, data: { 'currentPage': (currentPage + 1), 'pageSize': 5 }, success: function(data) { if (data. IsRedirect && data.

IsRedirect === true) { alert('must redirect to ' + data. RedirectUrl); location = 'http://www.google.com'; } else { var divs = ''; $("#hfId"). Val(''); $("#ResultsDiv").empty(); $.

Each(data. Results, function() { divs += formatResultFunction(this); }); $("#ResultsDiv"). Append(divs); $(".

Resultsdiv:even"). AddClass("resultseven"); $(". Resultsdiv").

Hover(function() { $(this). AddClass("resultshover"); }, function() { $(this). RemoveClass("resultshover"); }); $("#HfId").

Val(""); $("#HfId"). Val(data. Count); } } }); return false; } Now when you want to get the current clients: var clients = getEntities("Clients/GetClients", currentPage, formatClientsResult); and when you want to get the current drivers: var drivers = getEntities("Staff/GetDrivers", currentPage, formatDriversResult); Next let's consider the getClientbyId and getDriverById functions.

They could be simplified to: function getEntityById(data, url, formatResultFunction) { $. Ajax({ url: url, data: data, success: function(data) { formatResultFunction(data); $("#adddiv").show(); $("#ResultsDiv").hide(); $("#PagerDown").hide(); $("#ImageButtonDiv").hide(); } }); return false; } The same pattern could be used for the rest.

– Chendur Pandian Jul 4 '10 at 6:41 @Pandiya, yes the same pattern could be used. – Darin Dimitrov Jul 4 '10 at 6:54 I am learning quite lot of stuff from you... Thanks man... You really rock.. – Chendur Pandian Jul 4 '10 at 6:56 @Pandiya, always glad to help. – Darin Dimitrov Jul 4 '10 at 7:00 I tried yours but the callback function doesn't seem to work stackoverflow.

Com/questions/3174249/… – Chendur Pandian Jul 4 '10 at 9:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions