![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
这段ajax代码里面的这段是什么意思? 10
不懂的地方我已经给了注释了,求大神帮忙解答下,3q!!!varnet=newObject();//定义一个全局变量netnet.AjaxRequest=function(...
不懂的地方我已经给了注释了,求大神帮忙解答下,3q!!!
var net=new Object(); //定义一个全局变量net
net.AjaxRequest=function(url,onload,onerror,method,params){//创建一个构造函数
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
this.loadDate(url,method,params);
}
net.AjaxRequest.prototype.loadDate=function(url,method,params){
if (!method){
method="GET";
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}
if (this.req){//这段是什么意思
try{
var loader=this;
this.req.onreadystatechange=function(){
net.AjaxRequest.onReadyState.call(loader);
}
this.req.open(method,url,true);
//this.req.send(params);
this.req.send(null);
}catch (err){
this.onerror.call(this);
}
}
}
net.AjaxRequest.onReadyState=function(){ //重构onReadyState函数
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=function(){ //默认的错误处理函数
alert("error fetching data!"
+"\n\nreadyState:"+this.req.readyState
+"\nstatus: "+this.req.status
+"\nheaders: "+this.req.getAllResponseHeaders());
} 展开
var net=new Object(); //定义一个全局变量net
net.AjaxRequest=function(url,onload,onerror,method,params){//创建一个构造函数
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
this.loadDate(url,method,params);
}
net.AjaxRequest.prototype.loadDate=function(url,method,params){
if (!method){
method="GET";
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}
if (this.req){//这段是什么意思
try{
var loader=this;
this.req.onreadystatechange=function(){
net.AjaxRequest.onReadyState.call(loader);
}
this.req.open(method,url,true);
//this.req.send(params);
this.req.send(null);
}catch (err){
this.onerror.call(this);
}
}
}
net.AjaxRequest.onReadyState=function(){ //重构onReadyState函数
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=function(){ //默认的错误处理函数
alert("error fetching data!"
+"\n\nreadyState:"+this.req.readyState
+"\nstatus: "+this.req.status
+"\nheaders: "+this.req.getAllResponseHeaders());
} 展开
2个回答
展开全部
net.AjaxRequest.prototype.loadDate=function(url,method,params){ // 封装方法loadDate
//===================================
//这段用来创建XMLHttp对象
if (window.XMLHttpRequest){
if (!method){
method="GET";
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}
//===================================
// 这段操作XMLHttp对象发送Ajax请求,可参考XMLHttp使用文档
if (this.req){// 判断XMLHttp对象是否已生成
try{
var loader=this; // this 就是此函数本身,用loader是为了在下面的call中调用
this.req.onreadystatechange=function(){
net.AjaxRequest.onReadyState.call(loader); // 这里不能用this,因为在此this表示上层function
}
this.req.open(method,url,true);
//this.req.send(params);
this.req.send(null);
}catch (err){
this.onerror.call(this);
}
}
//===================================
}
//===================================
//这段用来创建XMLHttp对象
if (window.XMLHttpRequest){
if (!method){
method="GET";
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}
//===================================
// 这段操作XMLHttp对象发送Ajax请求,可参考XMLHttp使用文档
if (this.req){// 判断XMLHttp对象是否已生成
try{
var loader=this; // this 就是此函数本身,用loader是为了在下面的call中调用
this.req.onreadystatechange=function(){
net.AjaxRequest.onReadyState.call(loader); // 这里不能用this,因为在此this表示上层function
}
this.req.open(method,url,true);
//this.req.send(params);
this.req.send(null);
}catch (err){
this.onerror.call(this);
}
}
//===================================
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询