zoukankan      html  css  js  c++  java
  • Ajax登录实现

     1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
     2 
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4 
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head runat="server">
     7     <title></title>
     8     <script src="Scripts/common.js" type="text/javascript"></script>
     9     <script type="text/javascript">
    10         var xhr;
    11         window.onload = function () {
    12             xhr = createXmlHttp();
    13             gel("btnL").onclick = loginByAjax;
    14         }
    15         function loginByAjax() {
    16             var uName = gel("txtUName").value;
    17             var uPwd = gel("txtPwd").value;
    18             var urlStr = "DoLogin.ashx";///??????指定接收数据的页面
    19            //发送到后台数据的格式
    20             var data = "un=" + uName + "&up=" + uPwd;
    21             xhr.open("POST", urlStr, true);
    22             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    23             xhr.onreadystatechange = function () {
    24                 gel("msgDiv").innerHTML = "登录中......";
    25                 if (xhr.readyState == 4) {
    26                     if (xhr.status == 200) {
    27                         var reponseText = xhr.responseText;
    28                         if (reponseText == "1") {
    29                             alert("登录成功!");
    30                             window.location = "Default.aspx";
    31                         } else {
    32                             alert("登录失败!");
    33                         }
    34                     }
    35                     else {
    36                         alert("服务器错误!xhr.status=" + xhr.status);
    37                     }
    38                 }
    39             }
    40           //发送数据
    41             xhr.send(data);
    42         }
    43     </script>
    44 </head>
    45 <body>
    46 <center>
    47     <form id="form1" runat="server" action ="DoLogin.ashx">
    48     <div>
    49     <input type="text" id="txtUName" name="txtUName" />
    50     <input type="password" name ="txtPwd" id="txtPwd"/>
    51     <input type="button" id="btnL" value="登录" />
    52     </div>
    53     <div id="msgDiv"></div>
    54     </form>
    55 </center>
    56 </body>
    57 </html>

    后台页面DoLogin.ashx

     1 <%@ WebHandler Language="C#" Class="DoLogin" %>
     2 
     3 using System;
     4 using System.Web;
     5 
     6 public class DoLogin : IHttpHandler {
     7     string uName;
     8     string uPwd;
     9     public void ProcessRequest (HttpContext context) {
    10         
    11        uName = context.Request.Form["un"];//注意这里获取的是send发送过来的数据
    12        uPwd = context.Request.Form["up"];
    13         if ((uName == "yaoxinchao") &&( uPwd == "123456"))
    14         {
    15             System.Threading.Thread.Sleep(3000);
    16             context.Response.Write("1");
    17         }
    18     }
    19  
    20     public bool IsReusable {
    21         get {
    22             return false;
    23         }
    24     }
    25 
    26 }
  • 相关阅读:
    【老生常谈】Attr与Prop的区别
    【NodeJs】入门笔记一
    absolute的left和right的妙用
    文本框选中文字却不出现蓝色选中状态
    Ajax
    Canvas 与 SVG 的区别
    如何学好英语
    xCode6中SizeClass的使用
    CocoaPods安装和使用教程
    Unity3D教程宝典之Shader篇:特别讲 CG函数
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3117258.html
Copyright © 2011-2022 走看看