I am trying to loop through the JSON object and create an input type dynamically in a div
here is the Jquery function
$(document).ready(function (){
$.ajax({
type: "POST",
url: "Tables.aspx/TotaTable",
contentType: "application/json; charset=utf-8",
dataType:'JSON',
success: function(data) {
//var data= $.parseJSON(data);
$.each(data,function(index,jsonobj)
{
alert(jsonobj.OTID);
}
);
}
});
});
Here is the server side code that return the JSON values I am using json.net framework to convert my datatable to JSON
[System.Web.Services.WebMethod(EnableSession=true)]
public static string TotaTable()
{
LoginData lData = (LoginData)HttpContext.Current.Session["LData"];
ClsDataAccess cData = new ClsDataAccess();
DataTable dt = cData.GetTable("Select otid,ottableno from FBOUTLETTABLES where otoutletid=1");
string val=JsonConvert.SerializeObject(dt).ToString();
return val;
}
here is the JSON value that is sent to the client side
[{"OTID":76.0,"OTTABLENO":222.0},{"OTID":3.0,"OTTABLENO":3.0},{"OTID":4.0,"OTTABLENO":4.0},{"OTID":5.0,"OTTABLENO":5.0},{"OTID":6.0,"OTTABLENO":6.0},{"OTID":7.0,"OTTABLENO":7.0},{"OTID":8.0,"OTTABLENO":8.0},{"OTID":9.0,"OTTABLENO":9.0},{"OTID":2.0,"OTTABLENO":2.0},{"OTID":1.0,"OTTABLENO":1.0}]
My problem is
alert(jsonobj.OTID);
shows 'undefined'
I did try changing the server side function to return a List
like
public static List<Tables> Tables()
{
List<Tables> myList=new List<Tables>();
//Code to get datas from the database
foreach(DataRow row in dt.Rows)
{
myList.Add(new Tables{
tabID=row["tabID"].ToString();
tables=row["tab"].ToString();
});
}
return myList;
}
but I am facing the same issue..