jQuery非同期要求アナログIEログインサイト
20433 ワード
具体的な要求のログイン検証ページのバックグラウンド論理処理は、ここでは無視され、私たちの学習範囲内ではありません.重要なのは、jQuery非同期要求メソッドを使用することです.次の例です.
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head id="Head1" runat="server">
6 <title></title>
7 <script type="text/javascript" src="_layouts/Scripts/jquery-1.4.1.js"></script>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div id="loginDiv">
12 <asp:Label ID="lbl_LoginName" runat="server" Text=" :"></asp:Label><asp:TextBox
13 ID="txt_LoginName" runat="server" Text="searchstg"></asp:TextBox><br />
14 <asp:Label ID="lbl_Pwd" runat="server" Text=" :"></asp:Label><asp:TextBox ID="txt_Pwd"
15 runat="server" Text="portal@stG"></asp:TextBox><br />
16 <asp:Button ID="btn_Submit" runat="server" Text=" " OnClientClick="clearAuthCache();" />
17 <asp:Label ID="lbl_Info" runat="server"></asp:Label>
18 <hr />
19 <input type="button" id="btn_Login" value=" " onclick="doLogin();" />
20 <input type="button" id="btn_Logout" value=" " onclick="clearAuthCache();" />
21 </div>
22 </form>
23 <script type="text/javascript">
24 var clearAuthCache = function () {
25 if ('<%= isClearAuthCache %>'.toLowerCase() == 'true') {
26 try {
27 document.execCommand('ClearAuthenticationCache'); // IE 28 }
29 catch (e) {
30 }
31 finally {
32 }
33 }
34 };
35 //
36 var jq_AsynLogin = function (loginName, pwd, domain, asynUrl, successDefaultUrl) {
37 $.ajax({
38 type: 'POST',
39 url: asynUrl,
40 async: true,
41 global: false,
42 username: domain + '\\' + loginName,
43 password: pwd,
44 beforeSend: function (xmlHttpRequest) {
45 var imgStr = '<img src="/_Layouts/Styles/WorkPlatform/Blue/loading.gif" alt="" />';
46 $('#loginDiv').html(imgStr);
47 },
48 success: function (data, textStatus) {
49 window.location.href = successDefaultUrl;
50 },
51 complete: function () {
52 },
53 error: function (xmlHttpRequest, textStatus) {
54 if (xmlHttpRequest.status == '401') {
55 alert(' !');
56 window.location.href = window.location.href;
57 }
58 else {
59 alert(" , !");
60 }
61 }
62 });
63 };
64 // IE
65 var asynLogin = function (loginName, pwd, domain, asynUrl, successDefaultUrl) {
66 var auth = new ActiveXObject('msxml2.xmlhttp');
67 auth.open('post', asynUrl, false, domain + '\\' + loginName, pwd);
68 auth.send();
69
70 switch (auth.status) {
71 case 200:
72 window.location.href = successDefaultUrl;
73 break;
74 case 401:
75 alert(' !');
76 break;
77 default:
78 alert(' , !');
79 }
80 };
81 var doLogin = function () {
82 var loginNameObj = document.getElementById('<%= txt_LoginName.ClientID %>');
83 var pwdObj = document.getElementById('<%= txt_Pwd.ClientID %>');
84 if (loginNameObj.value == '') {
85 alert(' !');
86 loginNameObj.onfocus();
87 return;
88 }
89 if (pwdObj.value == '') {
90 alert(' !');
91 pwdObj.onfocus();
92 return;
93 }
94 jq_AsynLogin(loginNameObj.value, pwdObj.value, 'contoso', 'http://yxjt.contoso.com/', 'http://yxjt.contoso.com/WorkPlatform2/Pages/default.aspx');
95 };
96 </script>
97 </body>
98 </html>