Mark Tabor

Mark Tabor

  • 585
  • 2k
  • 460.2k

My AJAX is not binding the UL list item below is the code

Mar 21 2019 12:26 PM
Here is my html and ajax
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8" />  
  5. <title></title>  
  6. <script src="Scripts/jquery-3.3.1.min.js"></script>  
  7. <script type="text/javascript">  
  8. $(document).ready(function (){  
  9. var UserLists = $('#Userlist');  
  10. $('#btnUsers').click(function () {  
  11. $.ajax({  
  12. type: 'GET',  
  13. url: 'api/employees/',  
  14. dataType: 'json',  
  15. success: function (data) {  
  16. UserLists.empty();  
  17. $.each(data, function (index, val) {  
  18. var Username = val.Username;  
  19. UserLists.append('<li>' + Username+'</li>')  
  20. });  
  21. }  
  22. });  
  23. });  
  24. $('#btnClear').click(function () {  
  25. UserLists.empty();  
  26. });  
  27. });  
  28. </script>  
  29. </head>  
  30. <body>  
  31. <input type="button" id="btnUsers" value="GetUsersList"/>  
  32. <input type="button" id="btnClear" value="Clear Users"/>  
  33. <ul id="Userlist"/>  
  34. </body>  
  35. </html>  
My controller code is here
  1. public class EmployeesController : ApiController  
  2. {  
  3. [HttpGet]  
  4. public IEnumerable<User> Get()  
  5. {  
  6. using (LoginEntities ln = new LoginEntities())  
  7. {  
  8. return ln.Users.ToList();  
  9. }  
  10. }  
  11. [HttpGet]  
  12. public HttpResponseMessage Get(int id)  
  13. {  
  14. using (LoginEntities dd = new LoginEntities())  
  15. {  
  16. var entity = dd.Users.FirstOrDefault(e => e.UserId == id);  
  17. if (entity != null)  
  18. {  
  19. return Request.CreateResponse(HttpStatusCode.OK, entity);  
  20. }  
  21. else  
  22. {  
  23. return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee Id " + id.ToString() + "Not found");  
  24. }  
  25. }  
  26. }  
when i inspect the data is there but the UL is not binding and not showing the data on page there is no error on console Jquery is working fine i check it with alert.

Answers (12)