ajax完整结构:

$.ajax({
url:"",//服务器路径
data:{},//给服务端传递的参数,可以没有,也可以是多个
type:"post", //传递参数的方式,可post可get
dataType:"json",//数据传递的格式,有json和xml两种
success:function(data){},//成功返回执行这里,顺序为2
beforeSend:function(){},//数据发送前执行此方法,一触发ajax就触发此方法,顺序为1
complete:function(){},//数据返回后执行此方法,不管成功或失败,顺序为3
error:function(){}//服务器路径错误、或是服务器数据错误,走这里报错
  //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="js/jquery-1.7.2.min.js"></script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
用户名:<input type="text" id="txt1" />
<br />
密码:<input type="text" id="txt2" />
<br />
<span id="sp1" style="color: green;">请输入用户名和密码!</span>
<br />
<input type="button" id="btn1" value="登录" style="width: 130px; height: 40px;" />
</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> && pwd.value.trim().length>)
{
$.ajax({
url: "../ajax/Login.ashx",
data: {"uname":uname.value,"pwd":pwd.value},
type: "post",
dataType: "json",
success: function (rt) {
if (rt.has == "") {
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>
04-13 11:10