js封装ajax问题
这样的封装方式可以运行:
*********************************
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);
}