jquery訪問ashx文件示例代碼
.ashx 文件用于寫web handler的。.ashx文件與.aspx文件類似,可以通過它來調(diào)用HttpHandler類,它免去了普通.aspx頁面的控件解析以及頁面處理的過程。其實就是帶HTML和C#的混合文件。
.ashx文件適合產(chǎn)生供瀏覽器處理的、不需要回發(fā)處理的數(shù)據(jù)格式,例如用于生成動態(tài)圖片、動態(tài)文本等內(nèi)容。很多需要用到此種處理方式。此文檔提供一個簡單的調(diào)用ashx文件的Demo,并貼出關(guān)鍵文件的源碼。
以下為Demo中Login.ashx文件中的源碼:
public class Login : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "application/json"; //GET方式獲取傳遞的數(shù)據(jù) //string username = context.Request.QueryString["username"]; //string password = context.Request.QueryString["password"]; //POST方式獲取傳遞的數(shù)據(jù) 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格式非常重要,否則會執(zhí)行jquery的的error函數(shù) 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("執(zhí)行失敗"); } }); } </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>
相關(guān)文章
jquery控制listbox中項的移動并排序的實現(xiàn)代碼
listbox中項的移動并排序的jquery實現(xiàn)代碼,使用jquery與listbox的朋友可以參考下。2010-09-09遮罩層點擊按鈕彈出并且具有拖動和關(guān)閉效果(兩種方法)
實現(xiàn)點擊按鈕彈出遮罩層,點擊按鈕可以隨意拖動和關(guān)閉。接下來,通過本篇文章給大家介紹遮罩層點擊按鈕彈出并且具有拖動和關(guān)閉效果,需要的朋友可以參考下2015-08-08