.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中的源碼:
jsquery訪問ashx文件 聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com