JS
function Ajax(obj) { var instance = new Object(); //instance.XMLHttpReq; instance.url = obj.url; instance.backFunction = obj.callback; instance.cache = obj.cache == undefined ? false : obj.cache; //默认缓存为false // 创建XMLHttpRequest对象 createXMLHttpRequest = function() { if (window.XMLHttpRequest) {//Mozilla浏览器 instance.XMLHttpReq = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { instance.XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { instance.XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } }; // 发送请求函数 sendRequest = function(url) { this.createXMLHttpRequest(); //if (!instance.cache) { //instance.XMLHttpReq.setRequestHeader("If-Modified-Since", "0"); // } // 发送请求 instance.XMLHttpReq.open("GET", url, true); instance.XMLHttpReq.onreadystatechange = this.processResponse; //指定响应函数 instance.XMLHttpReq.send(null); }; //处理响应函数 processResponse = function() { if (instance.XMLHttpReq.readyState == 4) {//判断对象状态 if (instance.XMLHttpReq.status == 200) {// 信息已经成功返回,开始处理信息 instance.backFunction(instance.XMLHttpReq.responseText); instance = null; } else { //alert("您所请求的页面有异常"); instance = null; } } }; sendRequest(instance.url); }
调用函数
function checkLogin() { var uname = JsCore.$$("uname").value; var upass = JsCore.$$("upass").value; Ajax({ url: "/class/checkvcode.aspx?vcode=1&uname=" + uname + "&upass=" + upass + "&num=" + Math.random(), callback: function (data) { if (data != "") { if (data == "3") { location.href = "/member/default.aspx"; } if (data == "1") { //alert('用户名或密码输入错误!'); JsCore.$$("uname_msg").style.display = "block"; JsCore.$$("upass_msg").style.display = "block"; } } } }); }
调用页面
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.Data; using System.Data.SqlClient; namespace Web.Class { public partial class CheckVcode : System.Web.UI.Page { private string vcode = string.Empty; private string uname = string.Empty; private string upass = string.Empty; protected void Page_Load(object sender, EventArgs e) { if (Request["vcode"].ToString() != null && Request["vcode"].ToString() != "") { if (Request["uname"].ToString() == "" && Request["upass"].ToString() == "") { vcode = Request["vcode"].ToString().ToUpper(); string code = Session["CheckCode"].ToString().Trim().ToUpper(); if (vcode == code) { Response.Write("yes"); Response.End(); } else { Response.Write("no"); Response.End(); } } else { uname = Request["uname"].ToString(); upass = Request["upass"].ToString(); if (CheckLogin(uname, upass) == false) { Response.Write("1"); Response.End(); } else { if (CheckPass(uname, upass) == true) { //创建Cookie HttpCookie cookie = Request.Cookies["UserID"]; if (cookie == null) { cookie = new HttpCookie("UserID"); } cookie.Values.Set("ID", GetId(uname, upass)["ID"].ToString()); Response.SetCookie(cookie); Response.Write("3"); Response.End(); } } } } else { Response.Write("yes"); Response.End(); } } private bool CheckLogin(string uname,string upass) { StringBuilder sqlStr = new StringBuilder(); sqlStr.Append(@"select * from enterprise where orgcode=@uname and pwd=@upass"); SqlParameter[] parameter = { new SqlParameter("@uname",SqlDbType.VarChar), new SqlParameter("@upass",SqlDbType.VarChar) }; parameter[0].Value = uname; parameter[1].Value = upass; return DataHelper.Exists(sqlStr.ToString(), parameter); } private bool CheckNoPass(string uname, string upass) { StringBuilder sqlStr = new StringBuilder(); sqlStr.Append(@"select * from enterprise where orgcode=@uname and pwd=@upass and status=0"); SqlParameter[] parameter = { new SqlParameter("@uname",SqlDbType.VarChar), new SqlParameter("@upass",SqlDbType.VarChar) }; parameter[0].Value = uname; parameter[1].Value = upass; return DataHelper.Exists(sqlStr.ToString(), parameter); } private bool CheckPass(string uname, string upass) { StringBuilder sqlStr = new StringBuilder(); sqlStr.Append(@"select * from enterprise where orgcode=@uname and pwd=@upass"); SqlParameter[] parameter = { new SqlParameter("@uname",SqlDbType.VarChar), new SqlParameter("@upass",SqlDbType.VarChar) }; parameter[0].Value = uname; parameter[1].Value = upass; return DataHelper.Exists(sqlStr.ToString(), parameter); } private DataRow GetId(string uname, string upass) { StringBuilder sqlStr = new StringBuilder(); sqlStr.Append(@"select * from enterprise where orgcode=@uname and pwd=@upass"); SqlParameter[] parameter = { new SqlParameter("@uname",SqlDbType.VarChar), new SqlParameter("@upass",SqlDbType.VarChar) }; parameter[0].Value = uname; parameter[1].Value = upass; return DataHelper.QueryRow(sqlStr.ToString(), parameter); } } }