Ajax解析josn

前端之家收集整理的这篇文章主要介绍了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>
///
/// </summary>
private void getuserdetails(HttpContext context)
{
var id = Convert.ToInt32(context.Request["sid"]);
CodeModel.BLL.userinfo yhbll = new CodeModel.BLL.userinfo();
CodeModel.Model.userinfo yhModel = new CodeModel.Model.userinfo();
yhModel=yhbll.GetModel(int.Parse(id.ToString()));
context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(yhModel));

}

/// <summary>
///
/// </summary>
private void getMoneyusers(HttpContext context)
{
var id = context.Request["sid"].ToString();
CodeModel.BLL.userinfo yhbll = new CodeModel.BLL.userinfo();
CodeModel.Model.userinfo yhModel = new CodeModel.Model.userinfo();
IList<CodeModel.Model.userinfo> list = yhbll.GetUserList("type='"+id+"'");
if (list.Count > 0)
{
var Json = Newtonsoft.Json.JsonConvert.SerializeObject(list.Select(
t => new
{
yhid = t.id,
yhname = t.name,
yhusername=t.userName,
dizhi = t.place,

}));
context.Response.Write(Json);
}
}
C.sql

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

原文链接:https://www.f2er.com/ajax/161513.html

猜你在找的Ajax相关文章