I have an issue using XMLHttpRequest with typescript, here is a typescript class:
class Myclass {
constructor() {
this.init();
}
private init() {
const req = new XMLHttpRequest();
req.onreadystatechange = ( event: Event ): any => {
if (this.readyState === XMLHttpRequest.DONE) {
if (this.status === 200) {
this.render(this.responseText)
}
}
};
req.open('GET', '/api/test', true);
req.send(null);
};
private render( data:any ) {
console.log(`use ${data}`)
}
}
in this case 'this' will refer to the typescript class Myclass and using a javascript function 'this' will refers to the request and i will not be able to call my class method render().
how can i call my render() method and still have access to the response ?