AJAX阿贾克斯(二)★★★★★ajax的完整结构★★★★★

时间:2022-02-01 06:27:04

$.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="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>