这段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());
}
展开
 我来答
herrywood
2012-10-31 · TA获得超过870个赞
知道小有建树答主
回答量:523
采纳率:66%
帮助的人:460万
展开全部
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);
}
}
//===================================
}
ccppv
2012-11-06 · TA获得超过2994个赞
知道大有可为答主
回答量:5074
采纳率:0%
帮助的人:2801万
展开全部
if (this.req){//这段是什么意思

这一句上面不是创建了一个XMLHttpRequest对象了么。这里意思就是,如果对象存在,即已经创建成功,执行括号里面的代码。if是条件判断,如果后面的成立,则执行。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式