封装一个ajax类,回调函数出错了。

louloucindy 2010-07-08 05:15:28

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Ajax 封装</title>
<style type="text/css">

</style>
<script language="javascript">

function Ajax(surl,smenthod,sparas,basyn,fun){
this.url = surl;
this.menthod = smenthod;
this.paramts = sparas;
this.asynchronism = basyn;
if(typeof(fun) == "function"){
this.callfun = fun;
}else{

}
this.ajax =this.creatAjax();
}
Ajax.prototype.creatAjax = function(){
var xmlHttp=false;


if(!xmlHttp && typeof(XMLHttpRequest)!="undefined")
{
xmlHttp=new XMLHttpRequest();
}
return xmlHttp;
}
Ajax.prototype.callServer = function(){
var str='';
if(this.paramts){
var temp1 = this.paramts.split(",");
var len = temp1.length;
var temp2;
for(var i=0;i<len;i++){
temp2 = temp1[i].split(":");
if(i != len-1){
str += temp2[0]+"="+temp2[1]+"&";
}else{
str += temp2[0]+"="+temp2[1];
}
}
}
if(str.length > 0){
if(this.menthod == "GET"){
this.url = this.url+"?"+str;
}
}
this.ajax.open(this.menthod,this.url,this.asynchronism);
this.ajax.setRequestHeader("Content-Type","text/xml");
this.ajax.onreadystatechange = this.backFun;
if(this.menthod == "GET"){
this.ajax.send(null);
}else{
if(str.length >0 ){
this.ajax.send(str);
}
}
}
Ajax.prototype.backFun = function(){
if( this.readyState == 4 || this.readyState == 200){
var data = this.responseText;
this.callfun(sdata); //这行出错,this指向有问题
}
}
window.onload = function(){
var ajax1 = new Ajax("ajax.html","GET","",true,function(data){
document.getElementById("p1").innerHTML = data;
});
ajax1.callServer();
}

</script>
</head>
<body>
<p id="p1"></p>
</body>
</html>
...全文
130 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
优秀APP开发 2010-07-09
  • 打赏
  • 举报
回复
还自己去封装干嘛哟。。那么多现成的框架,就算你这次封装了,你一个月后还能再写出来吗?
Go 旅城通票 2010-07-09
  • 打赏
  • 举报
回复
你的代码好多错误啊~而且不兼容IE7-的浏览器~~

Ajax.prototype.creatAjax = function(){
var xmlHttp=false;

if( typeof(XMLHttpRequest)!="undefined")
{
xmlHttp=new XMLHttpRequest();
}else if(window.ActiveXObject)xmlHttp=new ActiveXObject("msxml2.xmlhttp.5.0");
if(!xmlHttp)throw new Exception("浏览器不支持Ajax!");
return xmlHttp;
}
Ajax.prototype.callServer = function(){
//....其他代码
this.ajax.onreadystatechange =function(me){//这样来添加状态转换函数,那么backFun中的this对象就指向Ajax的实例对象了,注意修改backFun对象中代码
return function(){
me.backFun();
}
}(this);

if(this.menthod == "GET"){
this.ajax.send(null);
}else{
//if(str.length >0 ){//加这个判断干吗?如果为post没传递参数就不发送请求了
this.ajax.send(str);
//}
}
}
Ajax.prototype.backFun = function(){
//if( this.readyState == 4 || this.readyState == 200){//是status属性吧?怎么两个readyState,并且status要readyState==4时才能访问,要不出错
if( this.ajax.readyState == 4){//注意此时this对象为Ajax的实例对象,而不是指向XMLHttpRequest这个对象
var data = this.ajax.responseText;
this.callfun(sdata);
}
}
louloucindy 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ibm_hoojo 的回复:]

JScript code

Ajax.prototype.backFun = function(){
if( this.readyState == 4 || this.readyState == 200){
var data = this.responseText;
//this.callfun(sdata); //这行出错,……
[/Quote]

我就是想在Ajax.prototype.backFun 里 调用 类的实例化实传进来的自定义函数,可就是怎么也不能引用,或是正确指向这个函数。
hoojo 2010-07-08
  • 打赏
  • 举报
回复

Ajax.prototype.backFun = function(){
if( this.readyState == 4 || this.readyState == 200){
var data = this.responseText;
//this.callfun(sdata); //这行出错,this指向有问题
//应该不是指向问题
this.callfun = fun() {};不是传递一个function么,怎么当方法使了
//or this.callfun = f; //f是一个方法名
}
}

hoojo 2010-07-08
  • 打赏
  • 举报
回复

Ajax.prototype.backFun = (function(a){
if( this.readyState == 4 || this.readyState == 200){
var data = this.responseText;
a.callfun(sdata); //这行出错,this指向有问题
}
})(Ajax);//这里传递你的实际ajax对象, var ajax1 = new Ajax
window.onload = function(){
var ajax1 = new Ajax("ajax.html","GET","",true,function(data){
document.getElementById("p1").innerHTML = data;
});
ajax1.callServer();
}

52,797

社区成员

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

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