zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    
    }

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




  • 相关阅读:
    WPF的模版
    AvalonDock结合MVVM模式的应用
    A Diagram Designer
    WPF Canvas小例子
    WPF ListView的使用及Linq to XML练习
    httpclient发送接受请求
    json序列化以及反序列化存在多个对象时候的处理
    json序列化
    wpf数据绑定
    wpf之WrapPanel与StackPanel
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5414101.html
Copyright © 2011-2022 走看看