国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jquery訪問ashx文件示例代碼

2019-11-20 14:17:06
字體:
來源:轉載
供稿:網友

.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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 从化市| 海伦市| 游戏| 定州市| 慈溪市| 通渭县| 衡山县| 玉田县| 洛阳市| 和田县| 汽车| 广州市| 淮南市| 建阳市| 辽源市| 壶关县| 汨罗市| 阿勒泰市| 封开县| 东港市| 会泽县| 富裕县| 耒阳市| 梓潼县| 呼图壁县| 镇沅| 共和县| 昆明市| 长宁县| 阿坝县| 鹿泉市| 武威市| 七台河市| 广南县| 锡林浩特市| 高唐县| 申扎县| 隆德县| 桐梓县| 湘潭县| 广昌县|