Ajax解析josn
A.js //查询一行数据 $("#Btsouso").bind("click",function () {var searchid = $("#txtids").val(); $.ajax({ type: "post", dataType: "json", url: "Mnet.ashx", cache: false, //async: false, data: { operate: "getuserdetails",sid: searchid }, success: function (text,textStatus) { if (text != null) { $("#results").html("用户名 :" + text.userName + " + 姓名 :" + text.name + " 地址 :" + text.place + " "); } }, complete: function (XMLHttpRequest,textStatus) { }, error: function (XMLHttpRequest,textStatus,errorThrown) { //alert(XMLHttpRequest.responseText); } }); }); //查询多行数据 $("#btgetmoney").bind("click", data: { operate: "getManyUser",textStatus) { if (text != null) { $.each(text,function (i,item) { $("#results").append("用户名 :" + item.yhusername + " + 姓名 : " + item.yhname + " 地址 :" + item.dizhi + "<br> "); }); } },errorThrown) { } }); });
ashx: /// <summary> //DataSet转换成List<ProductTypeInfo> public IList <CodeModel.Model.userinfo> GetUserList(string strwhere) { IList<CodeModel.Model.userinfo> yhlist = new List<Model.userinfo>(); CodeModel.BLL.userinfo yh = new BLL.userinfo(); //等待转换的DataSet DataSet ds = yh.GetList(strwhere); //遍历ds中Tables[0]数据 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//逐行遍历 i 是当前行 { CodeModel.Model.userinfo yhmodel = new Model.userinfo(); yhmodel.id = Convert.ToInt32(ds.Tables[0].Rows[i]["id"]); yhmodel.userName = ds.Tables[0].Rows[i]["userName"].ToString(); yhmodel.name = ds.Tables[0].Rows[i]["name"].ToString(); yhmodel.place = ds.Tables[0].Rows[i]["place"].ToString(); //将对象插入到List中 yhlist.Add(yhmodel); } //返回List return yhlist; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |