无法访问JSON数据

时间:2016-06-29 11:04:04

标签: c# asp.net

我正在尝试遍历JSON对象并在div中动态创建输入类型

这里是Jquery函数

$(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);
          }
          );
         }

    });   

});

这是返回JSON值的服务器端代码 我使用json.net框架将我的数据表转换为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;
   }

这是发送到客户端的JSON值

  

[{ “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}]

我的问题是 alert(jsonobj.OTID); 显示'undefined'

我确实尝试更改服务器端功能以返回List

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;
 }

但我面临同样的问题..

1 个答案:

答案 0 :(得分:0)

终于搞定了

这是Jquery代码

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);  
});
}
});

我还将服务器端代码更改为此

   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;


   }