服务器端程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ WebHandler Language= "C#" Class= "GetPrice" %> using System; using System.Web; public class GetPrice : IHttpHandler { public void ProcessRequest (HttpContext context) { context .Response.ContentType = "text/plain" ; string name = context.Request [ "name" ]; DataSetProductsTableAdapters .T_ProductsTableAdapter priceadapter= new DataSetProductsTableAdapters .T_ProductsTableAdapter(); DataSetProducts .T_ProductsDataTable prices = priceadapter.GetDataByName(name); if ( prices.Count <=0) { context .Response.Write( "none|0" ); } else { DataSetProducts .T_ProductsRow price = prices[0 ]; context .Response.Write( "ok|" +Convert.ToString(price .Price)); } } |
客户端页面代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | < script type = "text/javascript" > $( function () { $( "#Text1").blur(function () { var sname = $("#Text1" ).val(); $.post( "GetPrice.ashx", { name: sname }, function (data, status) { if (status == "success" ) { var arrs = data.split("|" ); if (arrs[0] == "ok" ) { $( "#Text2").val(arrs[1]); } else if (arrs[0] == "none") { alert( "没有该商品" ); } else { alert( "AJAX错误"); } } else { alert( "wrong"); } }); }); }) </ script > < body > < p > < input id = "Text1" type = "text" /> < input id = "Text2" type = "text" /></ p > </ body > </ html > |