js封装ajax问题

jixiaoqiang 2009-06-03 12:21:36
这样的封装方式可以运行:
*********************************
Default.aspx
<input type="button" value="查询" onClick="doQry();"/>
<script src="test.js"></script>
<script language="javascript">

function doQry()
{
var tmpObj=new oAjax();
tmpObj.send('Default2.aspx',function()
{
if(tmpObj.oSend.readyState==4 && tmpObj.oSend.status==200)
{
alert("你好:" + tmpObj.oSend.responseText);
}
else if(tmpObj.oSend.readyState==4 && tmpObj.oSend.status!=200){alert('error');}
})
}
</script>
************************************
test.js
function createXMLHttpRequest()
{
try
{
if (window.XMLHTTPRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
function oAjax()
{
this.oSend= new createXMLHttpRequest();
}
oAjax.prototype.send=function(url,handler)
{
this.oSend.open("GET",url,true);
this.oSend.onreadystatechange=handler;
this.oSend.setRequestHeader("If-Modified-Since","0");
this.oSend.send(null);
}

但是我现在在test.js中如果不要this.oSend对象,这样封装,提示出错,请问为什么?
*******************
default.aspx
<input type="button" value="查询" onClick="doQry();"/>
<div id="qryResult"></div>
<script src="test.js"></script>
<script language="javascript">
var m=new oAjax();
function doQry()
{
m.send('Default2.aspx',xx); //这里报错,说找不到m.send方法!!!!
}
function xx()
{

}
</script>

***********************
test.js

function oAjax()
{
return new this.createXMLHttpRequest();
}
oAjax.prototype.createXMLHttpRequest=function()
{
try
{
if (window.XMLHTTPRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
oAjax.prototype.zz=function(a)
{
return a;
}
oAjax.prototype.send=function(url,handler)
{
this.open("GET",url,true);
this.onreadystatechange=handler;
this.setRequestHeader("If-Modified-Since","0");
this.send(null);
}
...全文
107 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jixiaoqiang 2009-06-03
  • 打赏
  • 举报
回复
顶这么快?咋不发言呢? 高手帮忙.
jixiaoqiang 2009-06-03
  • 打赏
  • 举报
回复
楼上正解,类中必须有个变量保存ajax变量才行.
不过我自创的下面的写法更容易理解.更接近与C语言的Class
1.function即class 2.function不能直接嵌套function,必须声明个变量,然后赋值为函数
test.js
function Ajax()
{
this.request=function()
{
try
{
if (window.XMLHTTPRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
this.oAjax= new this.request();
this.send=function(url,handler)
{
this.oAjax.open("GET",url,true);
this.oAjax.onreadystatechange=handler;
this.oAjax.setRequestHeader("If-Modified-Since","0");
this.oAjax.send(null);
}
}

test.htm
<input type="button" value="查询" onClick="doQry();"/>
<script src="test.js"></script>
<script language="javascript">
function doQry()
{
var m=new Ajax();
m.send('default2.aspx',function()
{
if(m.oAjax.readyState==4 && m.oAjax.status==200)
{
alert("你好:" + m.oAjax.responseText);
}
else if(m.oAjax.readyState==4 && m.oAjax.status!=200){alert('error');}
})
}
</script>
monexus 2009-06-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 dh20156 的回复:]
你这样写肯定不行,先看这里:

//混乱的send方法
oAjax.prototype.send=function(url,handler){
this.open("GET",url,true);
this.onreadystatechange=handler;
this.setRequestHeader("If-Modified-Since","0");
this.send(null);
}


//构造函数中return object将覆盖new结果,改变实例的constructor从而无法保持prototype链
function oAjax(){
return new this.createXMLHttpReq…
[/Quote]

支持
dh20156 2009-06-03
  • 打赏
  • 举报
回复
你这样写肯定不行,先看这里:

//混乱的send方法
oAjax.prototype.send=function(url,handler){
this.open("GET",url,true);
this.onreadystatechange=handler;
this.setRequestHeader("If-Modified-Since","0");
this.send(null);
}


//构造函数中return object将覆盖new结果,改变实例的constructor从而无法保持prototype链
function oAjax(){
return new this.createXMLHttpRequest();
}

修正方法:


function oAjax(){
this.ajax = new this.createXMLHttpRequest();//将XMLHTTP对象赋给属性
}

oAjax.prototype.send=function(url,handler){//send方法不再混乱
this.ajax.open("GET",url,true);
this.ajax.onreadystatechange=handler;
this.ajax.setRequestHeader("If-Modified-Since","0");
this.ajax.send(null);
}

87,921

社区成员

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

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