Webmethod called via jquery ajax


/ Published in: C#
Save to your folder(s)

IMPORTANT: Be sure to clean the solution and rebuild after creating the WebMethod or you may get a 500 error.


Copy this code and paste it in your HTML
  1. [WebMethod]
  2. public static string DeleteCaseNewsItem(int ID)
  3. {
  4. SSHPortalDataClassesDataContext db = new SSHPortalDataClassesDataContext();
  5. var newsItem = (from n in db.CaseNews
  6. where n.CaseNewsID == ID
  7. select n).FirstOrDefault();
  8.  
  9. if (newsItem == null) return "false";
  10.  
  11. db.CaseNews.DeleteOnSubmit(newsItem);
  12. db.SubmitChanges();
  13.  
  14. return "true";
  15. }
  16.  
  17. //and the js
  18. $('.delete_file').click(function () {
  19. if (confirm('Delete this file?')) {
  20. $(this).parent().slideUp('slow');
  21. $.ajax({
  22. type: "POST",
  23. url: "Manage.aspx/DeleteUploadedFile",
  24. data: "{ID: '" + $(this).attr('rel') + "' }",
  25. contentType: "application/json; charset=utf-8",
  26. dataType: "json",
  27. success: function (msg) {
  28. if (msg.d != "true") alert('There was an error deleting the file.');
  29. },
  30. error: function (response) {
  31. var error = $.parseJSON(response.responseText);
  32. alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message);
  33. }
  34. });
  35. }
  36. return false;
  37. });
  38.  
  39. //---------------------------------
  40. //a totally separate example getting a complex object returned in JSON.
  41.  
  42. $(document).ready(function () {
  43. makeDropdownID = $('#ddModels').data('make-dropdown-id');
  44. $('#' + makeDropdownID).change(function () {
  45. $.ajax({
  46. type: "GET",
  47. url: "/services/Inventory.svc/GetModels",
  48. data: "inventoryType=A&make=" + $(this).val(),
  49. contentType: "application/json; charset=utf-8",
  50. dataType: "json",
  51. success: function (data) {
  52. if (data.d.length == 0) { //so user just unselected make and went back to "Select Make" on that dropdown. Or error occured and no models were found.
  53. $('#ddModels').empty().append('<option value="">Select Make First</option>');
  54. } else {
  55. $('#ddModels').empty().append('<option value="">Select Model</option>');
  56. }
  57.  
  58. $.each(data.d, function () {
  59. $('#ddModels')
  60. .append($('<option></option>')
  61. .attr('value', this.Name)
  62. .text(this.Name));
  63. });
  64. }
  65. });
  66. });
  67. });
  68.  
  69.  
  70. //Another separate example using JSON.stringify and json2.js library as a shim for older browsers that can't natively convert JS objects to JSON to send a complex object via ajax:
  71. $.ajax({
  72. url: "ManageSegment.aspx/GetPricingPreview",
  73. data: "{'pricingRuleRequest':" + JSON.stringify(GetPricingRuleRequest()) + "}",
  74. success: function (response) {
  75. RenderPreview(response.d);
  76. },
  77. error: function (response) {
  78. HandleAjaxError(response);
  79. }
  80. });
  81.  
  82. //Above example assumes ajax has been configured with default settings to keep the .ajax call simple, like this:
  83.  
  84. //Sets some default configuration options for ajax calls to make jQuery ajax calls shorter.
  85. function InitAjaxSetup() {
  86. $.ajaxSetup({
  87. type: "POST",
  88. contentType: "application/json; charset=utf-8",
  89. data: "{}",
  90. dataType: "json",
  91. error: function (response) {
  92. HandleAjaxError(response);
  93. }
  94. });
  95. }
  96.  
  97. //Handles any errors returned from ajax calls
  98. function HandleAjaxError(response) {
  99. var error = $.parseJSON(response.responseText);
  100. if (error.Message == 'Authentication failed.') {
  101. alert('Your session has timed out. You will now be redirected to the login page.');
  102. window.location.reload();
  103. return true;
  104. }
  105. alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message);
  106. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.