4
Answers

Not able to get the Integer value from ajax success function

Hiten Pandya

Hiten Pandya

7y
253
1
Hello all, I am new to Web API and try to get the HREmpID from my ajax response but unfortunetly I am getting undefined.Below screen shot will provide more information in case if you need.
 
 
 
And in the below code is of ajax code that will get the records from the api
 
 
  1. $(document).ready(function () {  
  2.         var r = new Array();  
  3.         var j = -1;  
  4.         r[++j] = '<table border = 1><thead><tr><th>HREmpID</th><th>Name</th><th>Address</th><th>City</th><th>Action</th></tr></thead><tbody>'  
  5.         $.ajax({  
  6.             type: "GET",  
  7.             url: "/api/EmployeeAPI/",  
  8.             success: function (result) {  
  9.                 //alert(result[0]["FirstName"]);  
  10.                 for (var i in result) {  
  11.                     var data = result[i];  
  12.                     alert(result[0]["HREmpID"]);  
  13.                     r[++j] = '<tr>';  
  14.                     r[++j] = '<td>';  
  15.                     r[++j] = data.HREmpID;  
  16.                     r[++j] = '</td>';  
  17.                     r[++j] = '<td>';  
  18.                     r[++j] = data.FirstName + " " + data.LastName;  
  19.                     r[++j] = '</td>';  
  20.                     r[++j] = '<td>';  
  21.                     r[++j] = data.Address;  
  22.                     r[++j] = '</td>';  
  23.                     r[++j] = '<td>';  
  24.                     r[++j] = data.City;  
  25.                     r[++j] = '</td>';  
  26.                     r[++j] = '</tr>';  
  27.                 }  
  28.                 r[++j] = '</tbody></table>';  
  29.                 $("#empDetails").html(r.join(''));  
  30.             }  
  31.         });  
  32.     });  
but I always getting the undefined for HRempID. I don't know what is going wrong ? any help will be appreciate. Thanks in advance.
Answers (4)
0
Ben Chris

Ben Chris

33 5.5k 1.1m 7y
Correct following lines in your above code I. E. for (var i in result) { var data = result[i]; To following I. E. foreach (var i in result) { var data = i; Also your property is "HREmpId" in the snippet not "HREmpID" which you have used in the html code.
Accepted
0
Hiten Pandya

Hiten Pandya

NA 318 1.5k 7y
Okay, I found that's a silly spelling mistake. Thanks for it.It's working now.
0
Hiten Pandya

Hiten Pandya

NA 318 1.5k 7y
 
Okay, here is the json response of list....
 
 
 
 
0
Ben Chris

Ben Chris

33 5.5k 1.1m 7y
Kindly share snippet of debugging with JSON version of your list response.