AJAX操作的JS类
最近用AJAX开始的时候是用网上下载的一个ajax.dll,但是似乎它不是异步处理的,应该有什么开关的吧,没找到文档,我也没研究,反正这东西也不难,就自己用js写了一个,呵呵,有兴趣的朋友看一下
///AJAX实现类
///acheqi 2005-12-3
function Js_Ajax()
{
this.PostUrl; ///AJAX提交的文件地址
this.Treat; ///参数返回后的处理过程
this.GetXmlHttpRequest=function()
{
var req;
if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
req = new XMLHttpRequest();
if (req.overrideMimeType) {
req.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!req)
{
alert("JS_CLASS:ERROR:组件创建失败")
return false;
}
return req;
}
this.SendRequest=function(url, params, HttpMethod)
{
if (!HttpMethod)
{
HttpMethod = "POST";
}
var Obj=this.GetXmlHttpRequest();
var s=this.processChech(Obj,this.Treat)
Obj.onreadystatechange = s
Obj.open(HttpMethod, url, true);
Obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
Obj.send(params);
}
this.Creat=function()
{
this.SendRequest(this.PostUrl,null,"GET")
}
this.processChech=function(mobject,f)
{
return function()
{
if (mobject.readyState == 4)
{
var state=mobject.status;
if (state==404)
{
alert("JS_CLASS:ERROR:请求页面不存在");
return false;
}
else if(state<200)
{
alert("JS_CLASS:ERROR:客户端错误:"+state.statusText);
return false;
}
else if(state==200)
{
f(mobject.responseText);
//return false;
}
}
}
}
}
var Ajax=new Js_Ajax();
调用方法
this.MemberEmail;
this.EmailValid=false;
this.NameValid=false;
/*检查email是否已经被注册*/
this.RequestMailIsReg=function(RequestData)
{
if (RequestData)
{
this.EmailValid=true;
}
else
{
this.EmailValid=false;
}
}
this.ClickEmailIsReg=function(Email)
{
this.MemberEmail=Js_System.prototype.GetItem(Email);
//alert(this.MemberEmail);
//检查格式是否正确
if (Js_String.prototype.ClickEmail(this.MemberEmail)==false)
{
return false;
}
else
{
Ajax.PostUrl="MemberReg.Aspx?Action=AjaxChk&Type=ChkEmail&Email="+this.MemberEmail;
Ajax.Treat=this.RequestMailIsReg;
Ajax.Creat();
if (this.EmailValid)
{
Js_String.prototype.ErrorStr='Email已经被注册!';
return false;
}
else
{
return true;
}
}
}
/*检查email是否已经被注册End*/