案例讲解asp.net中jquery post的用法
一、post案例:
1.前台default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>$.post发送请求</title> <script type="text/javascript" src="Jscript/jquery-1.4.2-vsdoc.js"> </script> <script type="text/javascript" src="Jscript/jquery-1.4.2.js"> </script> <style type="text/css"> body{font-size:13px} .divFrame{width:260px;border:solid 1px #666} .divFrame .divTitle{padding:5px;background-color:#eee;height:23px} .divFrame .divTitle span{float:left;padding:2px} .divFrame .divContent{padding:8px} .divFrame .divContent .clsShow{font-size:14px} select,input{float:left} .txt{border:#666 1px solid;padding:2px;width:80px;margin-right:3px} .btn {border:#666 1px solid;padding:2px;width:50px; filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffff, EndColorStr=#ECE9D8);} </style>
<script type="text/javascript"> $(function() { $("#Button1").click(function() { //按钮单击事件 //打开文件,并通过回调函数返回服务器响应后的数据 $.post("Handler.ashx", { name: encodeURI($("#txtName").val()), sex: encodeURI($("#selSex").val()) }, function(data) { $("#divTip") .empty() //先清空标记中的内容 .html(data); //显示服务器返回的数据 }) }) }) </script>
</head> <body> <div class="divFrame"> <div class="divTitle"> <span>姓名:</span> <input id="txtName" type="text" class="txt" /> <select id="selSex" style="height:22px;margin-right:3px"> <option value="">选性别</option> <option value="男">男</option> <option value="女">女</option> </select> <input id="Button1" type="button" class="btn" value="请求" /> </div> <div class="divContent"> <div id="divTip"></div> </div> </div> </body> </html>2.后台handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World");
string strName = System.Web.HttpUtility.UrlDecode(context.Request["name"]);//解码姓名字符 string strSex = System.Web.HttpUtility.UrlDecode(context.Request["sex"]);//解码性别字符
string strHTML = "<div class='clsShow'>"; //初始化保存内容变量 if (strName == "<span style="font-family: Arial, Helvetica, sans-serif;">陶国荣</span>" && strSex == "<span style="font-family: Arial, Helvetica, sans-serif;">男</span>") { strHTML += "姓名:陶国荣<br>"; strHTML += "性别:男<br>"; strHTML += "邮箱:[email protected]<hr>"; } else if (strName == "<span style="font-family: Arial, Helvetica, sans-serif;">李建洲</span>" && strSex == "<span style="font-family: Arial, Helvetica, sans-serif;">女</span>") { strHTML += "姓名:李建洲<br>"; strHTML += "性别:女<br>"; strHTML += "邮箱:[email protected]<hr>"; } strHTML += "</div>"; context.Response.Write(strHTML); //context.Response.Write("return");//如直接返回字符,前台function中的data则为纯字符串“return” } public bool IsReusable { get { return false; } } }
3.说明:
1.传递的参数中的
name和对应的参数值encodeURI($("#txtName").val()),在函数中即可以表示为{name:encodeURI($("#txtName").val()),}
形式,也可以加对双方加单引号,ashx文件都可以得到参数值
2.encodeURI($("#txtName").val())是对遇到汉字的处理形式,相应的后台需要使用System.Web.HttpUtility.UrlDecode(Request["name"])来进行汉字解码
3.后台尽量使用ashx文件响应,会使得的页面相应速度加快
4.如直接使用context.Response.Write("return"),前台function中的data将得到纯字符串“return”(不含html)
4.结果如图:
另外附加使用服务器控件的案例:
1.前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>$.post发送请求</title> <script type="text/javascript" src="Jscript/jquery-1.4.2-vsdoc.js"> </script> <script type="text/javascript" src="Jscript/jquery-1.4.2.js"> </script> <style type="text/css"> body{font-size:13px} .divFrame{width:260px;border:solid 1px #666} .divFrame .divTitle{padding:5px;background-color:#eee;height:23px} .divFrame .divTitle span{float:left;padding:2px} .divFrame .divContent{padding:8px} .divFrame .divContent .clsShow{font-size:14px} select,input{float:left} .txt{border:#666 1px solid;padding:2px;width:80px;margin-right:3px} .btn {border:#666 1px solid;padding:2px;width:50px; filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffff, EndColorStr=#ECE9D8);} </style> <script type="text/javascript"> $(function () { $("#Button1").click(function () { //按钮单击事件 //打开文件,并通过回调函数返回服务器响应后的数据 $.post("Handler.ashx", {
name: $("input[id*=txtName]").val(),
sex: $("#selSex").val() }, function (data) { $("#divTip") .empty() //先清空标记中的内容 .html(data); //显示服务器返回的数据 }) }) }) </script> </head> <body> <form runat=server> <div class="divFrame"> <div class="divTitle"> <span>姓名:</span>
<asp:TextBox ID="txtName" runat="server" class="txt" ></asp:TextBox>
<select id="selSex" style="height:22px;margin-right:3px"> <option value="">选性别</option> <option value="男">男</option> <option value="女">女</option> </select> <input id="Button1" type="button" class="btn" value="请求" /> </div> <div class="divContent"> <div id="divTip"></div> </div> </div> </form> </body> </html>
后台与前面案例相同:
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); string strName = System.Web.HttpUtility.UrlDecode(context.Request["name"]);//解码姓名字符 string strSex = System.Web.HttpUtility.UrlDecode(context.Request["sex"]);//解码性别字符 string strHTML = "<div class='clsShow'>"; //初始化保存内容变量 if (strName == "陶国荣" && strSex == "男") { strHTML += "姓名:陶国荣<br>"; strHTML += "性别:男<br>"; strHTML += "邮箱:[email protected]<hr>"; } else if (strName == "李建洲" && strSex == "女") { strHTML += "姓名:李建洲<br>"; strHTML += "性别:女<br>"; strHTML += "邮箱:[email protected]<hr>"; } strHTML += "</div>"; context.Response.Write(strHTML); //context.Response.Write("return"); } public bool IsReusable { get { return false; } } }
结果显示:
csdn的编辑器好像有点问题哦,着重号都变成了标签来表示,改了好几次,只好将着重部分单独分隔成一段了,希望大家见谅
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。