在没有类封装的情况下,代码什么问题都么有:
function startRequest(xmlHttp, sUrl, data) {
try {
xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
xmlHttp.setRequestHeader("Content-Length", data.length);
xmlHttp.onreadystatechange = function () {
readdata(xmlHttp);
};
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(data);
}
catch (exception) {
alert(exception);
}
}function readdata(xmlHttp){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
}
}
}但当我用MyObject()封装了代码。但跑到
xmlHttp.onreadystatechange = function () {
this.readdata(xmlHttp);
}
这个位置时就出报错了。
请问在类中,这里应该怎么写呢?
 
function MyObject(){ this.startRequest = function (xmlHttp, sUrl, data) {
try {
xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
xmlHttp.setRequestHeader("Content-Length", data.length);
HXML = xmlHttp
xmlHttp.onreadystatechange = function () {
this.readdata(xmlHttp);
};
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(data);
}
catch (exception) {
alert(exception);
}
}; this.readdata = function (xmlHttp) {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
}
}
}
}

解决方案 »

  1.   

    this.readdata(xmlHttp);
    this的问题,这里的this引用的是window
    不信将
    xmlHttp.onreadystatechange = function () {
    this.readdata(xmlHttp);
    }
    改成
    xmlHttp.onreadystatechange = function () {
    alert(this==window);//应该是true
    this.readdata(xmlHttp);
    }
      

  2.   

    //把xmlhttp定义成全局的变量.var xmlHttp=new XMLHttpRequest(); //其他的代码自己写了哈function startRequest( sUrl, data) {
    try {
    xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
    xmlHttp.setRequestHeader("Content-Length", data.length);
    xmlHttp.onreadystatechange = function () {
    readdata();
    };
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(data);
    }
    catch (exception) {
    alert(exception);
    }
    }function readdata(){
    if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
    var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
    }
    }
    }
      

  3.   

    to xray2005:
    阁下的方法lz早就会的,lz问的是怎么封装to lz:
    解决参数问题,一般可以用闭包
    function MyObject(){ this.startRequest = function (xmlHttp, sUrl, data) {
    try {
    xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
    xmlHttp.setRequestHeader("Content-Length", data.length);
    HXML = xmlHttp
    xmlHttp.onreadystatechange = this.bibao(this);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(data);
    }
    catch (exception) {
    alert(exception);
    }
    }; this.readdata = function (xmlHttp) {
    if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
    var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
    }
    }
    }
    this.bibao=function (obj){
    return function (){obj.readdata(xmlHttp);};
    };
    }
      

  4.   

    lihui_shine(浪尖賞花) 
    看起来这种写法成功机会挺大。我再试试!谢谢老兄。