Ajax类
Send方法的参数解释:
Ajax.Send(Url[, Async[, CallFunc[, User[, Pass]]]]);
Ajax:
必选项,Ajax对象的一个实例。
Url:
必选项,请求的地址。
Async:
可选项,为空表示请求模式为GET,为具体参数(如:{key : "postkey", value : "postvalue"})表示请求为POST。
CallFunc:
可选项,为空表示同步请求,为一个Function对象时表示异步请求并在Ajax的onreadystatechange事件中调用此函数。
User:
可选项,服务器需要验证时此参数为验证需要的用户名。
Pass:
可选项,服务器需要验证是此参数为验证需要的密码。
例子:
function getSend() {
if(ajax.xml.readyState == 4 && ajax.xml.status == 200){
alert(ajax.xml.responseText);
}
else {
alert("wait...");
}
}
var PostString = {
classid : 1,
search : "无忧脚本"
}
var ajax = new Ajax();
ajax.Send("http://bbs.51js.com/");//GET同步请求,可用ajax.xml.responseText获取返回值
ajax.Send("http://bbs.51js.com/", false, getSend);//GET异步请求,回调getSend()函数
ajax.Send("http://bbs.51js.com/", PostString);//POST同步请求,可用ajax.xml.responseText获取返回值
ajax.Send("http://bbs.51js.com/", PostString, getSend);//POST异步请求,回调getSend()函数
以下为程序代码:
function Ajax() {
this.xmlObject = function() {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
try {
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
return new XMLHttpRequest();
}
catch(e) {
return window.createRequest();
} ;
};
};
};
this.xml = this.xmlObject();
this.Send = function() {
var PostStr = !!arguments[1] ? (function() {
var tempArr = new Array();
for(var i in arguments.callee.caller.arguments[1]){
tempArr.push(i + "=" + arguments.callee.caller.arguments[1][i]);
}
return tempArr.join("&");
})() : null;
this.xml.open(PostStr ? "POST" : "GET", arguments[0], !!arguments[2], arguments[3], arguments[4]);
!!arguments[2] ? this.xml.onreadystatechange = arguments[2] : null;
if(PostStr){
this.xml.setRequestHeader("Content-Length", PostStr.length);
this.xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
};
this.xml.send(PostStr);
};
};