一、查询用户信息
服务端:
private MyDBDataContext _Context = new MyDBDataContext(); public void ProcessRequest (HttpContext context) { //接收数据 string s = context.Request["uid"].ToString(); //数据处理 var query = _Context.Login.Where(p=>p.UserName == s); string item = ""; if (query.Count() > 0) { Login data = query.First(); item += "<count>1</count>"; item += "<name>" + data.Name + "</name>"; item += "<password>" + data.Password + "</password>"; item += "<account>" + data.Account + "</account>"; } else { item += "<count>0</count>"; } //输出XML context.Response.Write("<?xml version = '1.0'?>"); context.Response.Write("<root>"); context.Response.Write(item); context.Response.Write("</root>"); context.Response.End();
客户端:
<script src="Script/jquery-1.7.1.min.js"></script> <script language="javascript"> $(document).ready(function () { $("#txt").keyup(function () { var s = $("#txt").val();//取用户名 $.ajax({ url: "Ajax/ShowLoginData.ashx", data: { uid: s }, type: "POST", dataType: "XML", success: function (data) { //解析 var c = $(data).find("count").text(); var ct = parseInt(c); if (c > 0) { var name = $(data).find("name").text(); var password = $(data).find("password").text(); var account = $(data).find("account").text(); //显示 $("#lblName").html(name); $("#lblPwd").html(password); $("#lblAccount").html(account); } else { $("#lblName").empty(); $("#lblPwd").empty(); $("#lblAccount").empty(); } }//success });//ajax });//keyup });//ready </script>
二、省市联动
服务端:
private MyDBDataContext _Context = new MyDBDataContext(); public void ProcessRequest (HttpContext context) { //接收数据 var parent = context.Request["parent"]; //数据处理 var query = _Context.ChinaStates.Where(p=>p.ParentAreaCode == parent); string item = null; StringBuilder builder = new StringBuilder(); if (query.Count() > 0) { foreach (ChinaStates data in query) { builder.Append("<item code='" + data.AreaCode + "' name='" + data.AreaName + "'/>"); } item = builder.ToString(); } //输出XML context.Response.Write("<?xml version = '1.0'?>"); context.Response.Write("<root>"); context.Response.Write(item); context.Response.Write("</root>"); context.Response.End();
客户端:
<script src="Script/jquery-1.7.1.min.js"></script> <script language="javascript"> $(document).ready(function () { FillProv();//加载省份 //给下拉列表加事件 $("#ddlProv").change(function () { FillCity();//点击省份出来对应的城市 }); $("#ddlCity").change(function () { FillCounty();//点击城市出来对应的区县 }); });//ready //省份 function FillProv() { $.ajax({ url: "Ajax/LianDong.ashx", data: { parent: "0001" }, type: "POST", dataType: "XML", success: function (data) { $("#ddlProv").empty(); //解析 var items = $(data).find("item"); for (var i = 0; i < items.length; i++) { var code = $(items).eq(i).attr("code"); var name = $(items).eq(i).attr("name"); //显示 var $op = $("<option value='" + code + "'>" + name + "</option>"); $("#ddlProv").append($op); } FillCity(); }//success });//ajax } //城市 function FillCity() { var prov = $("#ddlProv").val();//取值 $.ajax({ url: "Ajax/LianDong.ashx", data: { parent: prov }, type: "POST", dataType: "XML", success: function (data) { $("#ddlCity").empty(); //解析 var items = $(data).find("item"); for (var i = 0; i < items.length ; i++) { var code = $(items).eq(i).attr("code"); var name = $(items).eq(i).attr("name"); //显示 var $op = $("<option value='" + code + "'>" + name + "</option>"); $("#ddlCity").append($op); } FillCounty(); }//success });//ajax } //区县 function FillCounty() { var city = $("#ddlCity").val();//取值 $.ajax({ url: "Ajax/LianDong.ashx", data: { parent: city }, type: "POST", dataType: "XML", success: function (data) { $("#ddlCounty").empty(); var items = $(data).find("item"); for (var i = 0; i < items.length; i++) { var code = $(items).eq(i).attr("code"); var name = $(items).eq(i).attr("name"); //显示 var $op = $("<option value='" + code + "'>" + name + "</option>"); $("#ddlCounty").append($op); } }//success });//ajax } </script>