<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="SmartSystem_Website.Login" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="jQuery/style.css" rel="stylesheet" />
    <script src="jQuery/jquery-3.2.1.min.js"></script>
<script>
    $(document).ready(function (c) {
            $('.alert-close').on('click', function (c) {
                $('.message').fadeOut('slow', function (c) {
                    $('.message').remove();
                });
    });
            $("#button1").click(function () {
                //var username = $("#text1").text;
                //var password = $("#text2").text;
                //alert("ok");
                $.ajax({
                    url: 'Login.ashx',
                    type: 'POST',
                    data: { user: $("#text1").val(), pass: $("#text2").val() },
                    dataType: 'json',
                    //timeout: 50000,
                    //contentType: 'application/json;charset=utf-8',  
                    success: function (response) {
                        alert(response.message);
                        var rec = response.message;
                        //alert("success");
                    },
                    error: function (err) {
                        alert("请确认您的账号密码是否正确");
                    }
                });
                //$.ajax({
                //    url: 'Login.ashx',
                //    type: 'get',
                //    data: {},
                //    success: function (data) {
                //        alert('ok');
                //        alert(data);
                //    }
                //})
            });
    });
</script>
</head>
    <div align=center>
<div style=“width:960px; margin:0 auto; height:90px;”>
<script>
    (function () {
        var s = "_" + Math.random().toString(36).slice(2);
        document.write('<div id="' + s + '"></div>');
        (window.slotbydup = window.slotbydup || []).push({
            id: '730419',
            container: s,
            size: '960,90',
            display: 'inlay-fix'
        });
    })();
</script>
<script src="http://dup.baidustatic.com/js/os.js"></script>
<body>
    <div class="message warning">
            <div class="insert">
                <div class="login-head">
        <h1>Login Form</h1>
        <div class="alert-close"> </div> 
            </div>
        <form id="form1" runat="server">
            <li>
<input id="text1" type="text" class="text" value="Username" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Username';}"/><a href="#" class=" icon user"></a>
</li>
<div class="clear"> </div>
<li>
<input id="text2" type="password" value="Password" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Password';}"/> <a href="#" class="icon lock"></a>
</li>
<div class="clear"> </div>
<div class="submit">
<input type="submit" id="button1" value="Sign in" />
<h4><a href="#">Lost your Password ?</a></h4>
  <div class="clear">  </div>
</div>
         </form>
             </div>
        </div>
    <div class="clear"> </div>
    <div class="footer">
<p>Copyright &copy; 2017 <a target="_blank" href="http://www.baidu.com">[email protected]</a>.</p>
    </div>
    <div style="display:none">[email protected]</div>
</body>
</html>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;namespace SmartSystem_Website
{
    /// <summary>
    /// Login1 的摘要说明
    /// </summary>
    public class Login1 : IHttpHandler
    {
        private static string sql = "server=47.95.245.193;database=JerryDatabase ;uid=sa;pwd=Zxc123";
        static SqlConnection mysql = new SqlConnection(sql);
        SqlDataReader sdr = null;
        public void ProcessRequest(HttpContext context)
        {
            string rec = "ok";
            context.Response.ContentType = "text/plain";
            //context.Response.ContentEncoding = Encoding.UTF8;
            //context.Response.Write("Hello World");
            string username = context.Request.Form["user"];
            string password = context.Request.Form["pass"];
            //if (SelectUser(username))
            //{
            //    //if (SelectPass(password, username))
            //    //{
            //    context.Response.Write("{\"success\":true,\"message\":\"" + rec + "\"}");//此JSON格式非常重要,否则会执行jquery的的error函数                
            //    //else
            //    //    context.Response.Write("error");
            //}
            //else
            //{
            //    context.Response.Write("error");
            //}
            //context.Response.Write("进来过");
            //context.Response.Write("{\"success\":true,\"message\":\"" + rec + "\"}");//此JSON格式非常重要,否则会执行jquery的的error函数
            //context.Response.End();
            if (SelectUser(username))
            {
                context.Response.Write("{\"success\":true,\"message\":\"" + rec + "\"}");//此JSON格式非常重要,否则会执行jquery的的error函数                
            }
            //if (SelectPass(password, username))
            //{
            //    context.Response.Write("{\"success\":true,\"message\":\"" + rec + "\"}");//此JSON格式非常重要,否则会执行jquery的的error函数
            //}
            //rec = SelectPass(password, username);
            //context.Response.Write("{\"success\":true,\"message\":\"" + rec + "\"}");//此JSON格式非常重要,否则会执行jquery的的error函数
        }        public bool IsReusable
        {
            get
            {
                return false;
            }
        }        public bool SelectUser(string user)
        {
            string name = user;
            string name2 = "bbb";
            SqlCommand com = new SqlCommand("select UserName from UserInfo", mysql);
            //try
            //{
                mysql.Open();
                sdr = com.ExecuteReader();
                while (sdr.Read())
                {
                //for (int i = 0; i < sdr.FieldCount; i++)
                //{                    if (name == (sdr["UserName"].ToString().Trim()))
                    {
                        mysql.Close();
                        return true;
                     }
                //name = sdr["UserName"].ToString().Trim();                }
                //sdr.Close();
                //mysql.Close();
            //}
                sdr.Close();
                mysql.Close();
            //}
            //catch
            //{
            //    sdr.Close();
            //    mysql.Close();
            //}
            return false;
        }        public bool SelectPass(string password, string name)
        {
            string pass = password;
            string pass2 = "no";
            SqlCommand com = new SqlCommand("select Password from UserInfo where UserName=\'"+name+"\'", mysql);
            //try
            //{
                mysql.Open();
                sdr = com.ExecuteReader();
                while (sdr.Read())
                {
                //for (int i = 0; i < sdr.FieldCount; i++)
                //{
                    if (pass == (sdr["Password"].ToString().Trim()))
                    {
                        mysql.Close();
                        return true;
                    }
                //}
                //sdr.Close();
                //mysql.Close();
                }
                sdr.Close();
                mysql.Close();
            //}
            //catch
            //{
            //    mysql.Close();
            //}
            return false;
        }    }
}输入正确时,当第一次点击按钮时,不知道为什么就触发了error的函数,第二次点击才触发success函数