Ajax

hellogirl 2009-06-22 03:35:37
本人初次接触ajax,大家帮帮忙看下面的代码具体是什么意思,只能看懂创建XMLHttpRequest对象这部分。急啊,谢谢喽
我的分数就这么多,都给了
var net=new Object();
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){//创建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(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());
}
}

...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
huo789 2009-06-22
  • 打赏
  • 举报
回复
了解一下面向对象的Javascript就能看懂了
实际上就是定义了一个类:net.AjaxRequest
初始参数有:url,onload,onerror,method,params
包含方法:loadDate,onReadyState,defaultError

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧