using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
public partial class ajaxlogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string result = "-1";
string uName = Request.Form["un"];
string uPwd = Request.Form["up"];
if (!string.IsNullOrEmpty(uName) && !string.IsNullOrEmpty(uPwd))
{
BLL.StudentMananger bs = new StudentMananger();
Model.Students stu = bs.GetSingeByConition(uName, uPwd);
if (stu != null)
{
result ="1";
}
Response.Write(result);
Response.End();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajaxlogin.aspx.cs" Inherits="ajaxlogin" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="js/ajax.js" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function () {
// var xhr = createXmlHttp();
gel("Button1").onclick = onLogin;
}
function onLogin() {
var xhr = createXmlHttp();
var getname = gel("txtname").value;
var getpwd = gel("txtpwd").value;
var data = "un=" + getname + "&up=" + getpwd;
xhr.open("POST", "ajaxlogin.aspx", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var reponseText = xhr.responseText;
alert(reponseText);
if (reponseText == "1") {
alert("登录成功!");
window.location = "Default.aspx";
} else {
alert("登录失败!");
}
}
else {
alert("服务器错误!xhr.status=" + xhr.status);
}
}
}
xhr.send(data);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="txtname" type="text" /><input id="txtpwd" type="text" /><input
id="Button1" type="button" value="button" /></div>
</form>
</body>
</html>
function gel(cid) {
return document.getElementById(cid);
}
function createXmlHttp() {//创建xhr对象
var xhobj = false;
try {
xhobj = new ActiveXObject("Msxml2.XMLHTTP"); // ie msxml3.0+
} catch (e) {
try {
xhobj = new ActiveXObject("Microsoft.XMLHTTP"); //ie msxml2.6
} catch (e2) {
xhobj = false;
}
}
if (!xhobj && typeof XMLHttpRequest != 'undefined') {// Firefox, Opera 8.0+, Safari
xhobj = new XMLHttpRequest();
}
return xhobj;
}