Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问onreadystatechange函数中的类变量时出现Ajax类错误_Javascript_Php_Ajax - Fatal编程技术网

Javascript 访问onreadystatechange函数中的类变量时出现Ajax类错误

Javascript 访问onreadystatechange函数中的类变量时出现Ajax类错误,javascript,php,ajax,Javascript,Php,Ajax,我目前正在编写一个JavaScript Ajax类,遇到了一个错误。在函数processRawData()中,我似乎无法使用this.xhr访问类变量xhr。我得到“无法读取未定义的readyState的属性。我目前已通过在设置对onreadystatechange函数的引用时传入xhr值来修复此问题,但是这似乎是不必要的,因为我应该能够访问xhr值而不必这样做 function Ajax() { this.xhr = this.createXmlHttpRequest(); } Aj

我目前正在编写一个JavaScript Ajax类,遇到了一个错误。在函数processRawData()中,我似乎无法使用this.xhr访问类变量xhr。我得到“无法读取未定义的readyState的属性。我目前已通过在设置对onreadystatechange函数的引用时传入xhr值来修复此问题,但是这似乎是不必要的,因为我应该能够访问xhr值而不必这样做

function Ajax()
{
    this.xhr = this.createXmlHttpRequest();
}

Ajax.prototype.createXmlHttpRequest = function()
{
    if (window.XMLHttpRequest) {
        try {
            return new XMLHttpRequest();
        } catch (e) {
            throw new Error("Couldn't create XmlHttpRequest : " + e);
        }
    } else {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            throw new Error("Couldn't create XmlHttpRequest : " + e);
        }
    }
}

Ajax.prototype.request = function(type, url, params, dataType, callback)
{
    if (this.xhr.readyState === 0 || this.xhr.readyState === 4) {
        var isGetWithParams = (type === "GET") ? ((params !== null) ? url + params : url) : url
        this.xhr.open(type, isGetWithParams, true);

        this.xhr.onreadystatechange = this.processRawData(dataType, callback);

        var passInParams = (type === "GET") ? null : ((params !== null) ? params : null);
        this.xhr.send(passInParams);
    }
}

Ajax.prototype.processRawData = function(dataType, callback)
{
     return function()
     {
        if (this.xhr.readyState === 4 && this.xhr.status === 200) {
            switch (dataType) {
                case "text":
                    var data = this.xhr.responseText;
                    break;
                case "xml":
                default:
                    var data = this.xhr.responseXML;
            }

            callback(data);
        }
    }
}

看起来您的问题可能是因为在processRawData()中,您正在返回另一个函数并引用this.xhr.readyState,但“this”现在引用的是返回函数而不是Ajax类。请尝试:

Ajax.prototype.processRawData = function(dataType, callback){

var that = this; //'that' references the current Ajax instance

 return function()
 {
    if (that.xhr.readyState === 4 && that.xhr.status === 200) {...

与问题无关,但您应该知道,
Microsoft.XMLHTTP
分支的目的仅仅是支持IE版本5和6(更高版本可以使用
XMLHttpRequest
).只是说说而已。我真的怀疑这可能是问题所在。我可以发誓,当我之前将它从功能中取出时,它不起作用。谢谢你的帮助!