//success 和 error 只会执行其中一个
});
======================================================
ajax完整的登录
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="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" /> <script src="http://www.mamicode.com/js/jquery-1.7.2.min.js"></script> <title></title> </head> <body> <form runat="server"> <div> 用户名:<input type="text" /> <br /> 密码:<input type="text" /> <br /> <span>请输入用户名和密码!</span> <br /> <input type="button" value="登录" /> </div> </form> </body> </html> <script> document.getElementById("btn1").onclick = function () { var btn_login = document.getElementById("btn1"); var sp_login = document.getElementById("sp1"); var uname=document.getElementById("txt1"); var pwd=document.getElementById("txt2"); if(uname.value.trim().length>0 && pwd.value.trim().length>0) { $.ajax({ url: "../ajax/Login.ashx", data: {"uname":uname.value,"pwd":pwd.value}, type: "post", dataType: "json", success: function (rt) { if (rt.has == "1") { window.location.href = "MainFace.aspx"; sp_login.innerHTML = "登录成功!正在跳转..."; btn_login.value = "跳转中..."; } else { btn_login.removeAttribute("disabled"); sp_login.style.color = "red"; sp_login.innerHTML = "用户名或密码错误!请重新输入!"; btn_login.value = "登录"; } }, beforeSend: function () { btn_login.disabled = "disabled"; sp_login.style.color = "green"; btn_login.value = "登录中..."; }, error: function () { btn_login.removeAttribute("disabled"); sp_login.style.color = "red"; sp_login.innerHTML = "数据库连接异常!"; btn_login.value = "登录"; } }); } else { sp_login.style.color = "red"; sp_login.innerHTML = "用户名或密码不能为空!"; } }; </script>