1、index.aspx

在 这页中主要使用 $.ajax的使用

1、<body>部分
<div id="container" title="登陆窗口" style="width: 400px; height: 200px;"> <form id="loginForm" method="post"> <table> <tr> <th align="right"> 用户名 </th> <td> <input id="username" class="easyui-validatebox" data-options="required:true,missingMessage:'请输入用户名'" /> </td> </tr> <tr> <th align="right"> 密码: </th> <td> <input id="password" type="password" class="easyui-validatebox" data-options="required:true,missingMessage:'请输入密码'" /> </td> </tr> <tr> <td> <label id="errmsg" style="color: Red;"> </label> </td> </tr> </table> </form> </div>

2、<Script>部分
<script language="javascript" type="text/javascript" charset="utf-8">
        var dd;
        $(function () {
            dd = $('#container').dialog({
                closable: false,
                modal: true,
                buttons: [
                            { text: '登陆', handler: function () {
                                $.ajax({
                                    type: "POST",//post提交
                                    url: 'Handler.ashx?type=UserLogin', //注意这个地方的使用
                                    data: { username: $("#username").val(), password: $("#password").val() },
                                    dataType: 'json',//提交数据格式为'json'
                                    success: function (data) {
                                        if (data.result == 1) {
                                            dd.dialog('close');
                                            $.messager.alert('info', '登陆成功');
                                        } else {
                                            $.messager.alert('warning', '登陆失败');
                                            $("#errmsg").html(data.msg);
                                        }
                                    },
                                    error: function () {

                                    }
                                });
                            }
                            },
                            { text: '重置', handler: function () {
                                $("#username").attr("value", ""); //实现清除input部分
                                $("#password").attr("value", "");
                            }
                            }]
            });
        });

    </script>

 

2、Handler.ashx页面,主要处理 index页请求的内容,一般是新建一个 一般处理程序,来处理页面的请求

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/json";
        string type = HttpContext.Current.Request["type"];
        string responseText = "";
        switch (type)
        {
            case "UserLogin":
                responseText = UserLogin();
                break;
        }
        context.Response.Write(responseText);
    }
    private string UserLogin()
    {
        System.Data.DataTable dt;
        string username = HttpContext.Current.Request["username"];
      //在.cs文件中请求前台传的数据一般是 Request.QueryString["type"]
      //而在一般处理程序得使用这种 HttpContext.Current.Request["username"];
string password = HttpContext.Current.Request["password"]; ; BizFacade.HMS.HMS_loginSelect(username, password, out dt); string message = ""; if (dt.Rows.Count != 0) { return "{\"result\":1}"; } else { return "{\"result\":0,\"msg\":\"" + message + "\"}"; } } public bool IsReusable { get { return false; } } }

 3、实现结果