无法访问JSON数据

本文关键字:数据 JSON 访问 | 更新日期: 2023-09-27 17:52:57

我正在尝试通过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);显示"定义"

我确实尝试更改服务器端函数以返回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;
 }

但是我也面临同样的问题。

无法访问JSON数据

终于成功了

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;

   }