jquery訪問ashx檔案樣本

來源:互聯網
上載者:User

標籤:jquery   非同步請求ashx檔案   

.ashx 檔案用於寫web handler的。.ashx檔案與.aspx檔案類似,可以通過它來調用HttpHandler類,它免去了普通.aspx頁面的控制項解析以及頁面處理的過程。其實就是帶HTML和C#的混合檔案。

  .ashx檔案適合產生供瀏覽器處理的、不需要回傳處理的資料格式,例如用於產生動態圖片、動態文本等內容。很多需要用到此種處理方式。此文檔提供一個簡單的調用ashx檔案的Demo,並貼出關鍵檔案的源碼。

以下為Demo中Login.ashx檔案中的源碼:

public class Login : IHttpHandler {        public void ProcessRequest (HttpContext context) {        context.Response.ContentType = "application/json";        //GET方式擷取傳遞的資料        //string username = context.Request.QueryString["username"];        //string password = context.Request.QueryString["password"];        //POST方式擷取傳遞的資料        string username = context.Request.Form["username"];        string password = context.Request.Form["password"];        string message = null;        if (string.IsNullOrEmpty(username))        {            message = "使用者名稱不可為空";            context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");//此JSON格式非常重要,否則會執行jquery的的error函數            context.Response.End();        }        if (string.IsNullOrEmpty(password))        {            message = "密碼不可為空";            context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");            context.Response.End();        }        if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))        {            if (username.ToUpper() == "ADMIN" && password == "123")            {                                message = "登入成功";                context.Response.Write("{\"success\":true,\"message\":\"" + message + "\"}");            }            else            {                message = "使用者名稱或密碼錯誤";                context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");            }        }        context.Response.End();           }     public bool IsReusable {        get {            return false;        }    }}

以下為html中的源碼:

<!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>jsquery訪問ashx檔案</title>    <script language="javascript" type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>    <script language="javascript" type="text/javascript">        function login() {            $.ajax({                url: 'common/handler/Login.ashx',                type: 'POST',                data: { 'username': $("#txtUsername").val(), 'password': $("#txtPassword").val() },                dataType: 'json',                timeout: 50000,                //contentType: 'application/json;charset=utf-8',                success: function (response) {                                        alert(response.message);                },                error: function (err) {                    alert("執行失敗");                                    }            });        }    </script></head><body>    <div style="width:400px; height:300px; margin:0 auto; background:#c0c0c0;">        <dl style=" width:270px;">            <dd><span>使用者名稱:</span><input type="text" style=" width:150px;" id="txtUsername" /></dd>            <dd><span>密  碼:</span><input type="password" style=" width:150px;" id="txtPassword" /></dd>            <dd><input type="button" style=" width:65px; height:23px; float:right;" onclick="login()" value="登入" /></dd>        </dl>    </div></body></html>




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.