Can't retrieve result from AJAX objects with plain JavaScript -
trying implement own ajax prototype , not using jquery or other one. can't retrieve result, breakpoint, set on line doesn't fire:
the problem next part:
why undefined, if have init httpxml
instance init()
function:
i'm trying send request other part of program doing next:
var ajaxinstance = new gc3d.ajax(); ajaxinstance.init(); var response = ajaxinstance.sendrequest({ httpmethod: 'get', urlendpoint: '/someservice?function=somefunctionname' });
full source of 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 http request instance 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 problem request in gc3d.ajax.' ); } } }; gc3d.ajax.prototype.get = function() { return this.httprequest; };
what's incorrect , why isn't firing @ line above?
thanks
the problem this
context losing here:
else this.httprequest.onreadystatechange = this.getcontentfromrequest;
when i've applied event function above has lost instance.
let's imagine, don't know exact function name , anonymous function:
this.someevent.onsomething = function( item ) { ... };
and here it's losing in anonymous function because of scopes definition {}
, closing visible space this
.
so similar issue here code, when i've changed part to:
gc3d.ajax.prototype.getcontentfromrequest = function() { if ( this.readystate === 4 ) { if ( this.status === 200 ) return this.responsetext; else console.log( 'there problem request in gc3d.ajax.' ); } };
and code working!
Comments
Post a Comment