I am currently working on developing my own AJAX prototype without relying on jQuery or any other libraries. However, I am facing an issue where I cannot retrieve any results and the breakpoint set on a specific line is not triggering:
The problem seems to lie in the following part of the code:
Why is it showing as undefined, even though I have initialized the HttpXml
instance using the init()
function previously:
I am attempting to send a request from another part of the program by doing the following:
var ajaxInstance = new GC3D.Ajax();
ajaxInstance.init();
var response = ajaxInstance.sendRequest({
HttpMethod: 'GET',
UrlEndpoint: '/SomeService?function=someFunctionName'
});
This is the complete source code for the prototype:
GC3D.Ajax = function() {
this.httpRequest = undefined;
this.listExceptions = undefined;
};
GC3D.Ajax.prototype.init = function() {
this.listExceptions = [];
if ( window.XMLHttpRequest ) this.httpRequest = new XMLHttpRequest();
else if ( window.ActiveXObject ) {
try {
this.httpRequest = new ActiveXObject( 'Microsoft.XMLHTTP' );
}
catch ( exception ) {
this.listExceptions.push( exception );
try {
this.httpRequest = new ActiveXObject( 'Msxml2.XMLHTTP' );
}
catch ( exception ) {
this.listExceptions.push( exception );
try {
this.httpRequest = new ActiveXObject( 'Microsoft.XMLHTTP' );
}
catch ( exception ) {
this.listExceptions.push( exception );
}
}
}
}
if ( !this.httpRequest ) {
console.error( 'Can\'t create a HTTP Request instance for AJAX! Possible problems:' );
console.error( this.listExceptions );
}
else this.httpRequest.onreadystatechange = this.getContentFromRequest;
};
GC3D.Ajax.prototype.sendRequest = function( properties ) {
if ( this.httpRequest !== undefined ) {
this.httpRequest.open( properties.HttpMethod, properties.UrlEndpoint );
this.httpRequest.send();
}
else throw 'HTTP Request instance isn\'t defined!';
};
GC3D.Ajax.prototype.getContentFromRequest = function() {
if ( this.httpRequest !== undefined ) {
if ( this.httpRequest.readyState === 4) {
if ( this.httpRequest.status === 200 ) return this.httpRequest.responseText;
else console.log( 'There was a problem with the request in GC3D.Ajax.' );
}
}
};
GC3D.Ajax.prototype.get = function() {
return this.httpRequest;
};
Could you please point out what might be incorrect in the code causing it not to trigger at the line mentioned above?
Thank you