.net Ajax与后台一般处理程序(ashx) 交互
程序员文章站
2022-04-14 20:47:00
本文主要实现无动态刷新查询后台数据功能,主要用到ajax+ashx+sqlserver进行交互. 首先需要引用Jquery: html脚本: 前台通过一个事件来调用ashx: 后台来接收前台传过来的值,对其进行操作: SerializerHelper类的定义: 如果向后台传入多个参数在data里面用 ......
本文主要实现无动态刷新查询后台数据功能,主要用到ajax+ashx+sqlserver进行交互.
首先需要引用jquery:
<script language="javascript" type="text/javascript" src="../js/jquery.js">
html脚本:
<asp:textbox id="tb_corpname" runat="server" maxlength="100" width="369px"></asp:textbox>
前台通过一个事件来调用ashx:
<script type="text/javascript"> $(function () { $("#tb_corpname").blur(function () { //鼠标失去焦点事件 var corpname = $("#tb_corpname").val(); $.ajax({ type: "post", //提交方式 url: "/ashx/fzgpyshowdata.ashx", //一般处理程序的路径 data: { corpname: corpname }, //向后台传入的值 datatype: "json", //返回值格式 success: function (data) { //返回成功后将要做的事,这里是返回一个表 $("#tb_creditcode").val(data[0].creditcode); $("#tb_corpcode").val(data[0].corpcode); $("#tb_linktel").val(data[0].linkphone); $("#corpprovince option[text='" + data[0].statename + "']").attr("selected", true); showcity(); $("#corpcity option[text='" + data[0].adminareaname + "']").attr("selected", true); $("#tb_address").val(data[0].address); $("#tb_linkman").val(data[0].linkman); $("#tb_mobile").val(data[0].linkmobile); } }); }) }) </script>
后台来接收前台传过来的值,对其进行操作:
public void processrequest(httpcontext context) { context.response.contenttype = "text/plain"; string corpname = context.request["corpname"].trim().tostring(); //接收前台传过来的参数 if (corpname != "") { string sql = @"select top 1 corpname,creditcode,corpcode,linkphone,statename,adminareaname,linkman,linkmobile,address from tbproductorder as a inner join tbstatedic as b on a.statenum = b.statenum inner join tbadminareaclass on a.citynum = adminareaclassid where corpname =@corpname order by corpname"; sqlparameter[] par = new sqlparameter[1]; par[0] = new sqlparameter("@corpname", corpname); dataset ds = dbherpler.load(sql, par); string json = serializerhelper.tojsonstring(ds.tables[0]); //返回json类型的数据 context.response.write(json); context.response.end(); } }
serializerhelper类的定义:
using system; using system.collections.generic; using system.linq; using system.web; using system.text; using system.io; using system.xml.serialization; using newtonsoft.json; /// <summary> /// serializerhelper 的摘要说明 /// </summary> public static class serializerhelper { /// <summary> /// 反序列化xml文件 /// </summary> public static t loadfromxmlfile<t>(string filepath) where t : class { using (filestream stream = new filestream(filepath, filemode.open, fileaccess.read)) { xmlserializer serializer = new xmlserializer(typeof(t)); return (t)serializer.deserialize(stream); } } /// <summary> /// 反序列化xml字符串 /// </summary> public static t loadfromxmlstring<t>(string xml) where t : class { xmlserializer serializer = new xmlserializer(typeof(t)); byte[] bytes = encoding.utf8.getbytes(xml); using (memorystream stream = new memorystream(bytes)) { return (t)serializer.deserialize(stream); } } /// <summary> /// 序列化xml对象 /// </summary> public static string savetoxmlstring<t>(t entity) where t : class { using (memorystream stream = new memorystream()) { xmlserializer serializer = new xmlserializer(typeof(t)); serializer.serialize(stream, entity); return encoding.utf8.getstring(stream.toarray()); } } /// <summary> /// 序列化json对象 /// </summary> public static string tojsonstring(object obj) { return tojsonstring<object>(obj); } /// <summary> /// 序列化json对象 /// </summary> public static string tojsonstring<t>(t obj) where t : class { string text = jsonconvert.serializeobject(obj); return text; } /// <summary> /// 反序列化json字符串 /// </summary> public static t tojsonobject<t>(string text) where t : class { t obj = (t)jsonconvert.deserializeobject(text, typeof(t)); return obj; } }
如果向后台传入多个参数在data里面用逗号分割可写多个参数:
data: { corpname: corpname , corpname2: corpname2}