Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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 未捕获类型错误:对象#<;AbstractAjaxReq>;没有方法';getAjaxResponse';_Javascript_Ajax - Fatal编程技术网

Javascript 未捕获类型错误:对象#<;AbstractAjaxReq>;没有方法';getAjaxResponse';

Javascript 未捕获类型错误:对象#<;AbstractAjaxReq>;没有方法';getAjaxResponse';,javascript,ajax,Javascript,Ajax,我想在javascript中实现一些好的OOP实践。正如您所看到的,我有一个abstractAjaxRequest,然后给它一些定义其getAjaxResponse函数的子级。我在下面的这一行中看到了错误:self.getAjaxResponse()。有人看到我的错误吗 function soapReq() { var ajaxRequest = new SignInAjaxReq(); ajaxRequest.init(); var SOAPRequest = getS

我想在javascript中实现一些好的OOP实践。正如您所看到的,我有一个abstractAjaxRequest,然后给它一些定义其getAjaxResponse函数的子级。我在下面的这一行中看到了错误:
self.getAjaxResponse()
。有人看到我的错误吗

function soapReq() {
    var ajaxRequest = new SignInAjaxReq();
    ajaxRequest.init();
    var SOAPRequest = getSOAPHead();
    var bodyArgs = getLoginAttempt();
    SOAPRequest += getSOAPBody(bodyArgs[0], bodyArgs[1], bodyArgs[2]);
    SOAPRequest += getSOAPFoot();

    var url = 'xxx'
    ajaxRequest.ajaxRequest.open('POST', url, true);
    ajaxRequest.ajaxRequest.setRequestHeader( "Content-Type","text/xml; charset=utf-8");
    ajaxRequest.ajaxRequest.send(SOAPRequest);
}    

function AbstractAjaxReq() {
    var self = this;
    this.init = function() {
        self.ajaxRequest = new XMLHttpRequest();
        self.ajaxRequest.onreadystatechange = function() {
            if(self.ajaxRequest.readyState === 4){
                self.getAjaxResponse() // error here
            }
        };
        return self.ajaxRequest;
    };
};

function SignInAjaxReq() {
    var self = this;
    this.getAjaxResponse = function() {
        var xml = self.ajaxRequest.responseXML;
        var x=xml.getElementsByTagName("signInResult");
        for (i=0;i<x.length;i++) { 
            console.log(x[i].childNodes[0].nodeValue);
        }
    };
};
SignInAjaxReq.prototype = new AbstractAjaxReq();

Uncaught TypeError: Object #<AbstractAjaxReq> has no method 'getAjaxResponse' SoapRequest.js:42
    self.ajaxRequest.onreadystatechange
函数soapReq(){
var ajaxRequest=new SignInAjaxReq();
ajaxRequest.init();
var SOAPRequest=getSOAPHead();
var bodyArgs=getLoginAtTest();
SOAPRequest+=getSOAPBody(bodyArgs[0],bodyArgs[1],bodyArgs[2]);
SOAPRequest+=getSOAPFoot();
var url='xxx'
ajaxRequest.ajaxRequest.open('POST',url,true);
ajaxRequest.ajaxRequest.setRequestHeader(“内容类型”,“text/xml;charset=utf-8”);
ajaxRequest.ajaxRequest.send(SOAPRequest);
}    
函数AbstractAjaxReq(){
var self=这个;
this.init=函数(){
self.ajaxRequest=新的XMLHttpRequest();
self.ajaxRequest.onreadystatechange=函数(){
if(self.ajaxRequest.readyState==4){
self.getAjaxResponse()//此处出错
}
};
返回self.ajaxRequest;
};
};
函数signajaxreq(){
var self=这个;
this.getAjaxResponse=函数(){
var xml=self.ajaxRequest.responseXML;
var x=xml.getElementsByTagName(“signInResult”);

对于(i=0;i您的继承是有缺陷的。当您执行此操作时,
self
closure变量将指向该原型对象-它没有
getAjaxResponse
方法

修正1:使用:


此外,例如:

var abstractAjaxPrototype = {
    init: function() {
        var self = this; // here!
        this.ajaxRequest = new XMLHttpRequest();
        this.ajaxRequest.onreadystatechange = function() {
            if (self.ajaxRequest.readyState === 4){
                self.getAjaxResponse() // no more error here
            }
        };
        return this.ajaxRequest;
    }
};
function SignInAjaxReq() { }
SignInAjaxReq.prototype = Object.create(abstractAjaxPrototype);
SignInAjaxReq.prototype.getAjaxResponse = function() {
    var xml = this.ajaxRequest.responseXML;
    var x = xml.getElementsByTagName("signInResult");
    for (i=0; i<x.length; i++) { 
        console.log(x[i].childNodes[0].nodeValue);
    }
};

错误来自哪一行?这是问题所在。谢谢。
this.ajaxRequest.readyState
this
是函数作用域
this
,而不是抽象的ajaxreq。
this
-如果你知道我的意思:)
这个
并不是指你认为它能做什么Javascript不是c#或java。它实际上是一种支持原型继承(与面向对象不同)的函数式语言。因此,您所做的并不是真正的“良好OOP实践”。最好还是坚持使用javascript最佳实践IMHO。谢谢。有没有办法让它注意到我的childs getAjaxResponse()函数?我刚刚发布了4个修复程序,每个修复程序都可以让它工作:-)
function AbstractAjaxReq() {
    this.init = function() {
        var self = this; // here!
        self.ajaxRequest = new XMLHttpRequest();
        self.ajaxRequest.onreadystatechange = function() {
            if(self.ajaxRequest.readyState === 4){
                self.getAjaxResponse() // no more error here
            }
        };
        return self.ajaxRequest;
    };
};
var abstractAjaxPrototype = {
    init: function() {
        var self = this; // here!
        this.ajaxRequest = new XMLHttpRequest();
        this.ajaxRequest.onreadystatechange = function() {
            if (self.ajaxRequest.readyState === 4){
                self.getAjaxResponse() // no more error here
            }
        };
        return this.ajaxRequest;
    }
};
function SignInAjaxReq() { }
SignInAjaxReq.prototype = Object.create(abstractAjaxPrototype);
SignInAjaxReq.prototype.getAjaxResponse = function() {
    var xml = this.ajaxRequest.responseXML;
    var x = xml.getElementsByTagName("signInResult");
    for (i=0; i<x.length; i++) { 
        console.log(x[i].childNodes[0].nodeValue);
    }
};
function AbstractAjaxReq() {
    var self = this; // here!
    self.ajaxRequest = new XMLHttpRequest();
    self.ajaxRequest.onreadystatechange = function() {
        if(self.ajaxRequest.readyState === 4){
            self.getAjaxResponse() // no more error here
        }
    };
};
function SignInAjaxReq() {
    AbstractAjaxReq.call(this); // invoke the super constructor
}
SignInAjaxReq.prototype = Object.create(abstractAjaxPrototype);
SignInAjaxReq.prototype.getAjaxResponse = function() {
    var xml = this.ajaxRequest.responseXML;
    var x = xml.getElementsByTagName("signInResult");
    for (i=0; i<x.length; i++) { 
        console.log(x[i].childNodes[0].nodeValue);
    }
};

// Usage:
var ajaxRequest = new SignInAjaxReq();
// no ajaxRequest.init()!