Can't retrieve result from AJAX objects with plain JavaScript

Trying to implement own AJAX prototype and not using jQuery or other one. I can't retrieve any result, breakpoint, which is set on some line doesn't fire:

The problem is with the next part:

Why is it undefined, if I have previously init the HttpXml instance with the init() function:

I'm trying to send a request from other part of program doing the next:

var ajaxInstance = new GC3D.Ajax();
ajaxInstance.init();
var response = ajaxInstance.sendRequest({
    HttpMethod: 'GET',
    UrlEndpoint: '/SomeService?function=someFunctionName'
});

Full source of that 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;
};

What's incorrect and why it isn't firing at that line above?

Thanks

Answers


The problem was with the this context losing here:

else this.httpRequest.onreadystatechange = this.getContentFromRequest;

When I've applied for that event the function above it has lost this instance.

Let's imagine, we don't know the exact function name and it would be an anonymous function:

this.someEvent.onSomething = function( item ) { ... };

And here it's losing in anonymous function because of scopes definition {}, which closing the visible space for this.

So the similar issue is here with my code, when I've changed some part to:

GC3D.Ajax.prototype.getContentFromRequest = function() {
    if ( this.readyState === 4 ) {
        if ( this.status === 200 ) return this.responseText;
        else console.log( 'There was a problem with the request in GC3D.Ajax.' );
    }
};

And now code is working!


Need Your Help

Loop through COLUMN in MySQL and Servlet

mysql loops calculated-columns

I have a Servlet where I want to loop through my COLUMN "Allday_hours" in my MySQL database. Right now I only get the first COLUMN out printer out, but I want to loop through all the COLUMNS and AD...