Looking to incorporate OOP best practices into my javascript code. Currently, I have an abstractAjaxRequest with child classes defining the getAjaxResponse function. However, I am encountering an error on the line self.getAjaxResponse()
. Any ideas on how to resolve this issue?
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