用户:
BSS梅者如西查看:1 回复:4 评论:1 创建时间:2020-06-28T22:06:43
———————————————————————————————————————————————————————
var net=new Object(); //定义一个全局的变量
//编写构造函数
net.AJAXRRequest=fuction(url,onload,onerror,method,params){
this.req=null;
this.onload=onload;
this.onerrror=(onerror)?onerror:this.defaultRrror;
this.loadDate(url,method,params);
}
//编写用于初始化XMLHttpRequest对象并指定处理函数,最后发送HTTP请求的方法
net.AJAXRequest.prototype.loadDate=fuction(url,method,params){
if(!method){
method="GET" //设置默认的请求方式为GET
}
if(window.XMLHttpRequest){ //非IE浏览器
this.req=new XMLHttpRequest(); //创建XMLHttpRequest对象
}else if(window.ActiveXObject){ //IE浏览器
try{
this.req=new ActiveXobject("Microsoft.XMLHTTP"); //同上
} catch (e){
try{
this.req=new ActiveXObjcct("Msxml2.XMLHTTP"); //同上
} catch (e) {}
}
}
if(this.req){
try{
var loader=this;
this.req.onreadystatechange=function();
net.AJAXRequest.onReadyState.call(loader);
}
this.req.open(method,url,true); //建立对服务器的调用
if(method=="POST"){ //如果提交方式为MOST
this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //设置请求的内容类型
this.req.setRequestHeader("x-requested-with","ajax"); //设置请求的发出者
}
this.req.send(paranms); //发送请求
}catch (err){
this.onerror.call(this); //调用错误处理函数
}
}
}
//结构重调函数
net.AJAXRequest.onRradyState=fuction(){
var req=this.req;
var ready=req.readyState;
if(ready==4){
if(req.status==200){
this.onload.call(this);
}else{
this.onerror.call(this);
}
}
}
//重构默认的错误处理函数
net.AJAXRequest.prototype.defaultError=fuction(){
alert("错误数据/n/n回调状态:"+this.req.readyState+"/n状态:"this.req.status);
}
//这是我的JS书上讲的,不是我写的