Note that there are some explanatory texts on larger screens.

plurals
  1. POReusable jquery ajax requests
    primarykey
    data
    text
    <p>I am developing a web application using asp.net mvc... I am listing out the details of Clients,Staff,Reports via ajax requests using jquery... What i am doing is writing seperate functions(jquery ajax requests) for each actions (ie) view,add,edit,Delete ...</p> <pre><code>//Clients function getClients(currentPage) { $.ajax({ url: "Clients/GetClients", data: { 'currentPage': (currentPage + 1), 'pageSize': 5 }, contentType: "application/json; charset=utf-8", global: false, async: false, dataType: "json", beforeSend: function() { $('.loading').show(); }, complete: function() { $('.loading').hide(); }, success: function(data) { if (data.isRedirect &amp;&amp; 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 += '&lt;div class="resultsdiv"&gt;&lt;br /&gt;&lt;span style="display: inline-block;width:220px;" class="resultName"&gt;' + this.ClientName + '&lt;/span&gt;&lt;span class="resultfields" style="padding-left:10px;"&gt;Mobile No&amp;nbsp;:&lt;/span&gt;&amp;nbsp;&lt;span class="resultfieldvalues"&gt;' + this.ClientMobNo + '&lt;/span&gt;&lt;span style="float:right; padding-right:2px;"&gt;&lt;input type="checkbox" value=' + this.ClientId + ' onclick="storeIds();"/&gt;&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span class="resultfields"&gt;Address&amp;nbsp;:&lt;/span&gt;&amp;nbsp;&lt;span class="resultfieldvalues"&gt;' + this.ClientAddress + '&lt;/span&gt;&lt;/div&gt;'; }); $("#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; } //Drivers function getDrivers(currentPage) { $.ajax({ url: "Staff/GetDrivers", data: { 'currentPage': (currentPage + 1), 'pageSize': 5 }, contentType: "application/json; charset=utf-8", global: false, async: false, dataType: "json", beforeSend: function() { $('.loading').show(); }, complete: function() { $('.loading').hide(); }, success: function(data) { if (data.isRedirect &amp;&amp; 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 += '&lt;div class="resultsdiv"&gt;&lt;br /&gt;&lt;span style="display: inline-block;width:220px;" class="resultName"&gt;' + this.DriverName + '&lt;/span&gt;&lt;span class="resultfields" style="padding-left:10px;"&gt;Mobile No&amp;nbsp;:&lt;/span&gt;&amp;nbsp;&lt;span class="resultfieldvalues"&gt;' + this.DriverMobileNo + '&lt;/span&gt;&lt;span style="float:right; padding-right:2px;"&gt;&lt;input type="checkbox" value=' + this.DriverId + ' onclick="storeIds();"/&gt;&lt;/span&gt;&lt;/div&gt;'; }); $("#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; } //get client by id function getClientbyId(clientId) { $.ajax({ url: "Clients/getClientById", data: { 'clientId': clientId }, contentType: "application/json; charset=utf-8", global: false, async: false, dataType: "json", beforeSend: function() { $('.loading').show(); }, complete: function() { $('.loading').hide(); }, success: function(data) { $("#Name").val(data.ClientName); $("#MobileNo").val(data.ClientMobNo); $("#Address").val(data.ClientAddress); $("#hfEditId").val(data.ClientId); $("#adddiv").show(); $("#ResultsDiv").hide(); $("#PagerDown").hide(); $("#ImageButtonDiv").hide(); } }); return false; } //get driver by id function getDriverById(driverId) { $.ajax({ url: "Staff/getDriverById", data: { 'driverId': driverId }, contentType: "application/json; charset=utf-8", global: false, async: false, dataType: "json", beforeSend: function() { $('.loading').show(); }, complete: function() { $('.loading').hide(); }, success: function(data) { $("#Name").val(data.DriverName); $("#MobileNo").val(data.DriverMobileNo); $("#hfEditId").val(data.DriverId); $("#adddiv").show(); $("#ResultsDiv").hide(); $("#PagerDown").hide(); $("#ImageButtonDiv").hide(); } }); return false; } //clients delete function deleteClients(clientIds) { $.ajax({ url: "Clients/deleteClients", data: { 'ids': clientIds }, contentType: "application/json; charset=utf-8", global: false, async: false, dataType: "json", beforeSend: function() { $('.loading').show(); }, complete: function() { $('.loading').hide(); }, success: function(data) { if (data.Result == "Success") { getClients(0); var maxvalues = $("#HfId").val(); $(".pager").pagination(maxvalues, { callback: getClients, current_page: 0, items_per_page: 5, num_display_entries: 5, next_text: 'Next', prev_text: 'Prev', num_edge_entries: 1 }); return false; } } }); $("#alert").remove(); topBar('successfully deleted'); return false; } //delete drivers function deleteDrivers(driverIds) { $.ajax({ url: "Staff/deleteDrivers", data: { 'ids': driverIds }, contentType: "application/json; charset=utf-8", global: false, async: false, dataType: "json", beforeSend: function() { $('.loading').show(); }, complete: function() { $('.loading').hide(); }, success: function(data) { if (data.Result == "Success") { getDrivers(0); var maxvalues = $("#HfId").val(); $(".pager").pagination(maxvalues, { callback: getDrivers, current_page: 0, items_per_page: 5, num_display_entries: 5, next_text: 'Next', prev_text: 'Prev', num_edge_entries: 1 }); return false; } } }); $("#alert").remove(); topBar('successfully deleted'); return false; } </code></pre> <p>How to make these functions into a single function and just pass the values to the function... Is there way to do this...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload