jquery访问ashx文件示例

转自原文 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;
        }
    }
 
}
View Code

以下为html中的源码:

<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>
 
 
    <div style="width:400px; height:300px; margin:0 auto; background:#c0c0c0;">
        <dl style=" width:270px;">
            <dd><span>用户名:</span><input style=" width:150px;" id="txtUsername" type="text"></dd>
            <dd><span>密  码:</span><input style=" width:150px;" id="txtPassword" type="password"></dd>
            <dd><input style=" width:65px; height:23px; float:right;" οnclick="login()" value="登录" type="button"></dd>
        </dl>
     
 
 
</div>
View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值