標籤:ssr 使用者 路徑 click osi ges led stat else
ajax完整結構:
$.ajax({
url: "", //伺服器路徑
data: { }, //傳遞的參數,可為空白,可多個
type: "post", //傳遞參數的方式,可POST可GET,一般用POST
dataType: "json", //資料傳遞的格式,有Json和xml兩種
success: function (data) { //成功返回資料執行這裡,排第2
},
beforeSend: function () { //一觸發ajax就執行,無任何延遲,排第1
},
complete: function () { //所有的方法都執行完畢後再來執行這裡,排最後(不管成功失敗都會執行)
},
error: function () { //伺服器路徑錯誤,或是伺服器內部錯誤,走這裡報錯,此位置與success只會走一個
}
});
ajax完整結構樣本:
aspx檔案:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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> <style> * { margin: 0px; padding: 0px; } #div-load { position: fixed; top: 0px; left: 0px; z-index: 100000000000; width: 100%; height: 100%; background-image: url(images/touming.jpg); display: none; } #div-load img { float: left; } </style></head><body> <form id="form1" runat="server"> <div> 使用者名稱:<input type="text" id="uname" /><br /> 密碼:<input type="password" id="pwd" /><br /> <input type="button" id="btn1" style="width: 150px; height: 50px; font-size: 18px;" value="登入" /> <span id="sp1" style="color: red;"></span> </div> <div id="div-load"> <img src="images/Loading6.gif" /> <div>資料拚命載入中...</div> </div> </form></body></html><script type="text/javascript"> document.getElementById("btn1").onclick = function () { var un = document.getElementById("uname").value; var pwd = document.getElementById("pwd").value; $.ajax({ url: "ajax/Login.ashx", data: { "uname": un, "pwd": pwd }, type: "post", dataType: "json", success: function (data) { if (data.ok == "1") { window.location.href = "Default2.aspx"; document.getElementById("btn1").setAttribute("disabled", "disabled"); document.getElementById("btn1").value = "正在跳轉,請稍後..."; } else { document.getElementById("sp1").innerHTML = "使用者名稱或密碼錯誤!"; document.getElementById("btn1").removeAttribute("disabled"); document.getElementById("btn1").value = "登入"; } }, beforeSend: function () { document.getElementById("sp1").innerHTML = ""; document.getElementById("btn1").setAttribute("disabled", "disabled"); document.getElementById("btn1").value = "登入中..."; document.getElementById("div-load").style.display = "block"; }, complete: function () { document.getElementById("div-load").style.display = "none"; }, error: function () { document.getElementById("sp1").innerHTML = "伺服器串連失敗!"; document.getElementById("btn1").removeAttribute("disabled"); document.getElementById("btn1").value = "登入"; } }); };</script>
ashx檔案:
<%@ WebHandler Language="C#" Class="Login" %>using System;using System.Web;using System.Web.SessionState;public class Login : IHttpHandler, IRequiresSessionState{ public void ProcessRequest(HttpContext context) { System.Threading.Thread.Sleep(3000); string end = "{\"ok\":\"0\"}"; string uname = context.Request["uname"]; string pwd = context.Request["pwd"]; Users u = new UsersData().SelectUser(uname, pwd); if (u != null) { context.Session["user"] = uname; end = "{\"ok\":\"1\"}"; } context.Response.Write(end); context.Response.End(); } public bool IsReusable { get { return false; } }}
ajax完整結構