L A

L A

  • NA
  • 170
  • 171.2k

jQuery AJAX always executing error: {}

Oct 28 2017 5:23 PM

Hello everyone,

I'm working on a webapplication, ASP.Net MVC 4.0 with entityframework 6.0, trying to update database as per user selection. Data is sent to controller's action using jQuery AJAX. Below given is C# code to update entity which in turn updates database.
  1. public void modidyProduct(Productdetail prodData)  
  2. {  
  3.     try  
  4.     {  
  5.         using (SampleTrialEntities entity = new SampleTrialEntities())  
  6.         {  
  7.             var data = entity.Productdetails.Where(p=>p.ProductID == prodData.ProductID).FirstOrDefault<Productdetail>();  
  8.             data.ProductName = prodData.ProductName;  
  9.             data.ProductNumber = prodData.ProductNumber;  
  10.             data.CategoryName = prodData.CategoryName;  
  11.             data.ModelName = prodData.ModelName;  
  12.             entity.Entry(data).State = System.Data.Entity.EntityState.Modified;  
  13.             entity.SaveChanges();  
  14.         }  
  15.     }  
  16.     catch (Exception)  
  17.     {}  
  18. }  
And here's jQuery AJAX call for that controller action method.
  1. function updateProduct() {  
  2.             var productData = {  
  3.                 ProductName: $('#prodName').val().trim(),  
  4.                 ProductNumber: $('#prodNum').val().trim(),  
  5.                 CategoryName: $('#ctgryName :selected').text(),  
  6.                 ModelName: $('#mdlName :selected').text(),  
  7.                 ProductID: atob($('#editProductId').val())  
  8.             };  
  9.             debugger;  
  10.             $('#divLoader').show();  
  11.             $.ajax({  
  12.                 url: '@Url.Action("modidyProduct", "Home")',  
  13.                 data: JSON.stringify(productData),  
  14.                 type: 'POST',  
  15.                 dataType: 'json',  
  16.                 contentType: 'application/json;charset=utf-8',  
  17.                 success: function (jqXHR) {  
  18.                     //Below line will destroy DataTable - tblProducts. So that we could bind table again. next line - loadData();  
  19.                     $('#tblProducts').DataTable().destroy();  
  20.                     $('#divLoader').hide();  
  21.                     loadData();  
  22.                     $('#addModal').modal('hide');  
  23.                     $('#editProductId').val('');  
  24.                 },  
  25.                 error: function (msg) {  
  26.                     debugger;  
  27.                     $('#editProductId').val('');  
  28.                     $('#divLoader').hide();  
  29.                     alert(msg);  
  30.                     alert("What's going wrong ?");  
  31.                     //alert(jqXHR.responseText);  
  32.                 }  
  33.             });  
  34.         }  
  1. function loadData() {  
  2.             $('#divLoader').show();  
  3.   
  4.             $.ajax({  
  5.                 url: '@Url.Action("getProductDetails", "Home")',  
  6.                 type: 'GET',  
  7.                 dataType: 'json',  
  8.                 success: function (data) {  
  9.                     $('#tblProducts').DataTable({  
  10.                         data: data,  
  11.                         columns: [  
  12.                             { 'data''ProductName' },  
  13.                             { 'data''ProductNumber' },  
  14.                             { 'data''CategoryName' },  
  15.                             { 'data''ModelName' },  
  16.                             {  
  17.                                 'data''ProductID'"orderable"false"render"function (data) {  
  18.                                     return '<a class="btn btn-warning" data-toggle="tooltip" id="deleteRecord" title="Delete"><i class="glyphicon glyphicon-trash"></i></a> <a class="btn btn-info" data-toggle="tooltip" id="editRecord" title="Edit" onclick= "getProductById(' + data + ',this);"><i class="glyphicon glyphicon-edit"></i></a>';  
  19.                                 }  
  20.                             }]  
  21.                     });  
  22.                     $('#divLoader').hide();  
  23.                 },  
  24.                 error: function () {  
  25.                     alert('Server couldn`t process request');  
  26.                 }  
  27.             });  
  28.         }  
After executing jQuery AJAX method & controllers action, successfully updates the record in datbase. Response statuscode - 200 & Status - OK is returned. But only error: { }, code block is executed everytime in AJAX method.
 
It would be really helpful, if you guys post your comment/ response. Thanks in advance.

Answers (2)