【.Net码农】 jquery访问ashx文件示例

http://blog.csdn.net/burongwawa520/article/details/38471839



.ashx 文件用于写web handler的。.ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程。其实就是带HTML和C#的混合文件。

  .ashx文件适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片动态文本等内容。很多需要用到此种处理方式。此文档提供一个简单的调用ashx文件的Demo,并贴出关键文件的源码。

以下为Demo中Login.ashx文件中的源码:

[csharp]  view plain copy
  1. public class Login : IHttpHandler {  
  2.       
  3.     public void ProcessRequest (HttpContext context) {  
  4.         context.Response.ContentType = "application/json";  
  5.         //GET方式获取传递的数据  
  6.         //string username = context.Request.QueryString["username"];  
  7.         //string password = context.Request.QueryString["password"];  
  8.   
  9.         //POST方式获取传递的数据  
  10.         string username = context.Request.Form["username"];  
  11.         string password = context.Request.Form["password"];  
  12.         string message = null;  
  13.         if (string.IsNullOrEmpty(username))  
  14.         {  
  15.             message = "用户名不能为空";  
  16.             context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");//此JSON格式非常重要,否则会执行jquery的的error函数  
  17.             context.Response.End();  
  18.         }  
  19.         if (string.IsNullOrEmpty(password))  
  20.         {  
  21.             message = "密码不能为空";  
  22.             context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");  
  23.             context.Response.End();  
  24.         }  
  25.         if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))  
  26.         {  
  27.             if (username.ToUpper() == "ADMIN" && password == "123")  
  28.             {                  
  29.                 message = "登录成功";  
  30.                 context.Response.Write("{\"success\":true,\"message\":\"" + message + "\"}");  
  31.             }  
  32.             else  
  33.             {  
  34.                 message = "用户名或密码错误";  
  35.                 context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");  
  36.             }  
  37.         }  
  38.         context.Response.End();  
  39.          
  40.     }  
  41.    
  42.     public bool IsReusable {  
  43.         get {  
  44.             return false;  
  45.         }  
  46.     }  
  47.   
  48. }  

以下为html中的源码:

[html]  view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <title>jsquery访问ashx文件</title>  
  6.     <script language="javascript" type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>  
  7.     <script language="javascript" type="text/javascript">  
  8.         function login() {  
  9.             $.ajax({  
  10.                 url: 'common/handler/Login.ashx',  
  11.                 type: 'POST',  
  12.                 data: { 'username': $("#txtUsername").val(), 'password': $("#txtPassword").val() },  
  13.                 dataType: 'json',  
  14.                 timeout: 50000,  
  15.                 //contentType: 'application/json;charset=utf-8',  
  16.                 success: function (response) {                      
  17.                     alert(response.message);  
  18.                 },  
  19.                 error: function (err) {  
  20.                     alert("执行失败");                      
  21.                 }  
  22.   
  23.             });  
  24.         }  
  25.     </script>  
  26. </head>  
  27. <body>  
  28.     <div style="width:400px; height:300px; margin:0 auto; background:#c0c0c0;">  
  29.         <dl style=" width:270px;">  
  30.             <dd><span>用户名:</span><input type="text" style=" width:150px;" id="txtUsername" /></dd>  
  31.             <dd><span>密  码:</span><input type="password" style=" width:150px;" id="txtPassword" /></dd>  
  32.             <dd><input type="button" style=" width:65px; height:23px; float:right;" onclick="login()" value="登录" /></dd>  
  33.         </dl>  
  34.     </div>  
  35. </body>  
  36. </html>  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值