Ben john
Ben john

Reputation: 55

Not able to access JSON datas

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..

Upvotes: 0

Views: 91

Answers (1)

Ben john
Ben john

Reputation: 55

Finally Got It working

Here is the Jquery Code

var obj={};
obj.ID=id;   
 $.ajax({
    type:"POST",
    url:"Tables.aspx/ItemsFromCategory",
    data: JSON.stringify(obj),
    contentType: "application/json; charset=utf-8",
    dataType:'json'    

      }).done(function(res) {  

     var resultAsJson = res.d;
 $.each(resultAsJson, function (index, resObject) {
 alert(resObject.ID);  
});
}
});

I did also change the server side code to this

   public static List<ItemClass> ItemsFromCategory(string ID)
   {
       LoginData lData = (LoginData)HttpContext.Current.Session["LData"];

       System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
       List<ItemClass> myList = new List<ItemClass>();

      if (lData != null)
       {
           ClsDataAccess cData = new ClsDataAccess();
           DataTable dt = cData.GetTable("Select itmid,ITMNAME from FBITEMDETAILS where itmtype='I' and itmisprimary='N' and itmoutletid=" + lData.fbOutLetid + " and itmgrpid=" + ID);
          // DataTable dt = cData.GetTable("Select itmid,ITMNAME from FBITEMDETAILS where itmtype='I' and itmisprimary='N' and itmgrpid=" + ID);
           foreach (DataRow row in dt.Rows)
           {
               myList.Add(new ItemClass
               {
                   ID = row["itmid"].ToString(),
                   itemName=row["ITMNAME"].ToString(),

               });

           }
          // result = ser.Serialize(myList);
       //    return result;
           return myList;
       }
      // return string.Empty;
       return myList;


   }

Upvotes: 0

Related Questions