案例講解asp.net中jquery post的用法

來源:互聯網
上載者:User

案例講解asp.net中jquery post的用法

一、post案例:

1.前台default.aspx:

 

<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>
    <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>
姓名:  選性別男女  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);
<%@ webhandler="" language="C#" class="Handler">
        string strName = System.Web.HttpUtility.UrlDecode(context.Request[name]);//解碼姓名字元        string strSex = System.Web.HttpUtility.UrlDecode(context.Request[sex]);//解碼性別字元
        string strHTML = 
; //初始化儲存內容變數 if (strName == 陶國榮 && strSex == 男) { strHTML += 姓名:陶國榮
; strHTML += 性別:男
; strHTML += 郵箱:tao_guo_rong@163.com; } else if (strName == 李建洲 && strSex == 女) { strHTML += 姓名:李建洲
; strHTML += 性別:女
; strHTML += 郵箱:xiaoli@163.com; } strHTML +=; 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">   
    <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>       <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><%@ page="" language="C#" autoeventwireup="true" codefile="Default.aspx.cs" inherits="_Default">
                        
                              選性別                 男                 女                                              
                          
後台與前面案例相同:

 

 

<%@ 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 = 
; //初始化儲存內容變數 if (strName == 陶國榮 && strSex == 男) { strHTML += 姓名:陶國榮
; strHTML += 性別:男
; strHTML += 郵箱:tao_guo_rong@163.com; } else if (strName == 李建洲 && strSex == 女) { strHTML += 姓名:李建洲
; strHTML += 性別:女
; strHTML += 郵箱:xiaoli@163.com; } strHTML +=; context.Response.Write(strHTML); //context.Response.Write(return); } public bool IsReusable { get { return false; } } }

結果顯示:

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.