CRUD operations on SharePoint 2013 List Using RESTfull service

HTML Controls Code: 
  1. <table>  
  2. <tr>  
  3. <td>Employee ID</td>  
  4. <td>  
  5. <input type="text" id="txtId" /></td>  
  6. </tr>  
  7. <tr>  
  8. <td>Employee Name</td>  
  9. <td>  
  10. <input type="text" id="txtName" /></td>  
  11. </tr>  
  12. <tr>  
  13. <td colspan="2">  
  14. <input type="button" id="btnAdd" onclick="Add()" value="Add" />  
  15. <input type="button" id="btnGetEmployees" onclick="View()" value="View" />  
  16. <input type="button" id="btnUpdate" onclick="Update()" value="Update" />  
  17. <input type="button" id="btnDelete" onclick="Delete()" value="Delete" />  
  18. </td>  
  19. </tr>  
  20. </table>  
JavaScript Code (REST Service Code):
  1. // Add item ----------------  
  2. function Add() {  
  3. var listName = 'EmpList';  
  4. var url = _spPageContextInfo.siteAbsoluteUrl;  
  5. var title = $('#txtName').val();  
  6. var id = $('#txtId').val();  
  7. createListItemWithDetails(listName, url, title, id, function () {  
  8. alert("Item has been created. Updating available items");  
  9. $("#eData").empty();  
  10. View();  
  11. }, function (sender,args) {  
  12. alert("Ooops, an error occured. Please try again");  
  13. });  
  14. }  
  15. // Add Item -----------------  
  16. function createListItemWithDetails(listName, siteUrl, title, id, success, failure) {  
  17. var itemType = GetItemTypeForListName(listName);  
  18. var item = {  
  19. "__metadata": { "type": itemType },  
  20. "Title": title, "EmpId": id  
  21. };  
  22. $.ajax({  
  23. url: siteUrl + "/sales/_api/web/lists/getbytitle('" + listName + "')/items",  
  24. type: "POST",  
  25. contentType: "application/json;odata=verbose",  
  26. data: JSON.stringify(item),  
  27. headers: {  
  28. "Accept""application/json;odata=verbose",  
  29. "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  30. },  
  31. success: function (data) {  
  32. success(data);  
  33. },  
  34. error: function (data) {  
  35. failure(data);  
  36. }  
  37. });  
  38. }  
  39. // Common method ----------------  
  40. function GetItemTypeForListName(name) {  
  41. return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";  
  42. }  
  43. // Delete item --------------------  
  44. function Delete() {  
  45. var listName = 'EmpList';  
  46. var url = _spPageContextInfo.siteAbsoluteUrl;  
  47. var itemId = $('#txtId').val();  
  48. deleteListItem(itemId, listName, url, function () {  
  49. alert("Item deleted, refreshing avilable items");  
  50. $("#eData").empty();  
  51. View();  
  52. }, function () {  
  53. alert("Ooops, an error occured. Please try again");  
  54. });  
  55. }  
  56. // Delete item --------------------  
  57. function deleteListItem(itemId, listName, siteUrl, success, failure) {  
  58. getListItemWithId(itemId, listName, siteUrl, function (data) {  
  59. $.ajax({  
  60. url: data.__metadata.uri,  
  61. type: "POST",  
  62. headers: {  
  63. "Accept""application/json;odata=verbose",  
  64. "X-Http-Method""DELETE",  
  65. "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  66. "If-Match": data.__metadata.etag  
  67. },  
  68. success: function (data) {  
  69. success(data);  
  70. },  
  71. error: function (data) {  
  72. failure(data);  
  73. }  
  74. });  
  75. },  
  76. function (data) {  
  77. failure(data);  
  78. });  
  79. }  
  80. // Get required item to delete and update ---------------  
  81. function getListItemWithId(itemId, listName, siteurl, success, failure) {  
  82. var url = siteurl + "/sales/_api/web/lists/getbytitle('" + listName + "')/items?$filter=EmpId eq " + itemId;  
  83. $.ajax({  
  84. url: url,  
  85. method: "GET",  
  86. headers: { "Accept""application/json; odata=verbose" },  
  87. success: function (data) {  
  88. if (data.d.results.length == 1) {  
  89. success(data.d.results[0]);  
  90. }  
  91. else {  
  92. failure("Multiple results obtained for the specified Id value");  
  93. }  
  94. },  
  95. error: function (data) {  
  96. failure(data);  
  97. }  
  98. });  
  99. }  
  100. // View item ---------------  
  101. function View() {  
  102. // var itemId = $('#txtId').val();  
  103. var url = _spPageContextInfo.siteAbsoluteUrl + "/sales/_api/web/lists/getbytitle('EmpList')/items";  
  104. $.ajax({  
  105. url: url,  
  106. method: "GET",  
  107. contentType: "application/json;odata=verbose",  
  108. headers: { "Accept""application/json; odata=verbose" },  
  109. success: function (data) {  
  110. $("#eData").empty();  
  111. if (data.d.results.length >= 1) {  
  112. var r = data.d.results;  
  113. for (var i = 0; i < r.length; i++) {  
  114. $("#eData").append("<li>" + r[i].EmpId + " - " + r[i].Title + "</li>");  
  115. }  
  116. }  
  117. else {  
  118. alert("Zero records...");  
  119. // failure("Multiple results obtained for the specified Id value");  
  120. }  
  121. },  
  122. error: function (data) {  
  123. // failure(data);  
  124. alert("Error occoured...");  
  125. }  
  126. });  
  127. }  
  128. function Update() {  
  129. var listName = 'EmpList';  
  130. var url = _spPageContextInfo.siteAbsoluteUrl;  
  131. var itemId = $('#txtId').val();  
  132. var name = $('#txtName').val();  
  133. updateListItem(itemId, listName, url, name, function () {  
  134. alert("Item updated, refreshing avilable items");  
  135. $("#eData").empty();  
  136. View();  
  137. }, function () {  
  138. alert("Ooops, an error occured. Please try again");  
  139. });  
  140. }  
  141. function updateListItem(itemId, listName, siteUrl, name, success, failure) {  
  142. var itemType = GetItemTypeForListName(listName);  
  143. var item = {  
  144. "__metadata": { "type": itemType },  
  145. "Title": name  
  146. };  
  147. getListItemWithId(itemId, listName, siteUrl, function (data) {  
  148. $.ajax({  
  149. url: data.__metadata.uri,  
  150. type: "POST",  
  151. contentType: "application/json;odata=verbose",  
  152. data: JSON.stringify(item),  
  153. headers: {  
  154. "Accept""application/json;odata=verbose",  
  155. "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  156. "X-HTTP-Method""MERGE",  
  157. "If-Match": data.__metadata.etag  
  158. },  
  159. success: function (data) {  
  160. success(data);  
  161. },  
  162. error: function (data) {  
  163. failure(data);  
  164. }  
  165. });  
  166. }, function (data) {  
  167. failure(data);  
  168. });  
  169. }