Sanjay gautam

Sanjay gautam

  • NA
  • 90
  • 14.4k

web api on ajax call

Sep 29 2019 8:59 AM
I have some code it genrate request and hit api controller but did not return response and hit ajax succes or errror function  please suggest me where am i wrong
  1. <script type="text/javascript">  
  2.        $(document).ready(function () {  
  3.            var ann = { Name: 'Sanjay', Address: 'Kanpur' };  
  4.            var bob = { Name: 'Jitendra Mehlavat', Address: 'Delhi' };  
  5.            var list = [ann, bob];  
  6.            var myjson = JSON.stringify(list);  
  7.            $('#btn').on('click', function () {  
  8.                $.ajax({  
  9.                    url: '/api/employee/',  
  10.                    data: myjson,  
  11.                    dataType: 'json',  
  12.                    contentType: 'application/json; charset=utf-8',  
  13.                    type: "POST",  
  14.                    Succes: function (response,data) {  
  15.                        //alert('success');  
  16.                        var resdata = JSON.parse(response.d);  
  17.   
  18.                        alert(resdata);  
  19.                    },  
  20.                    Error: function (response) {  
  21.                        alert('fail');  
  22.                    },  
  23.                });  
  24.            });  
  25.        });  
  26.    </script>  
  1. public class employeeController : ApiController  
  2.    {  
  3.        [HttpPost]  
  4.          
  5.        public string Post(List<dataarr1> list)  
  6.        {  
  7.            return JsonConvert.SerializeObject(list);  
  8.   
  9.             
  10.        }  
  1. model class  
  2.   
  3. public class dataarr1  
  4.     {  
  5.         public string Name { getset; }  
  6.         public string Address { getset; }  
  7.     }  
  8.     public class RootObject  
  9.     {  
  10.     public List<dataarr1> list { getset; }  
  11.     }  
  1. public static void Register(HttpConfiguration config)  
  2.         {  
  3.             // Web API configuration and services  
  4.   
  5.             // Web API routes  
  6.             config.MapHttpAttributeRoutes();  
  7.   
  8.             config.Routes.MapHttpRoute(  
  9.                 name: "DefaultApi",  
  10.                 routeTemplate: "api/{controller}/{id}",  
  11.                 defaults: new { id = RouteParameter.Optional }  
  12.             );  
  13.         }  

Answers (4)