I am trying to write an Ajax class using TypeScript. TypeScript Code -
class Ajax { url: string; xmlData: string; mode: bool; response: string; objHttpReq:any; constructor (postUrl: string, postXml: string, postMode: bool) { this.url = postUrl; this.xmlData = postXml; this.mode = postMode; this.objHttpReq = new XMLHttpRequest(); this.objHttpReq.mode = this.mode; this.objHttpReq.onreadystatechange = this.OnRStateChange; this.objHttpReq.open("Post", this.url, this.mode); this.objHttpReq.send(this.xmlData); } OnRStateChange(){ if (this.readyState==4 && this.status==200)
Relevant javascript above code
var Ajax = (function () { function Ajax(postUrl, postXml, postMode) { this.url = postUrl; this.xmlData = postXml; this.mode = postMode; this.objHttpReq = new XMLHttpRequest(); this.objHttpReq.mode = this.mode; this.objHttpReq.onreadystatechange = this.OnRStateChange; this.objHttpReq.open("Post", this.url, this.mode); this.objHttpReq.send(this.xmlData); } Ajax.prototype.OnRStateChange = function () { if(this.readyState == 4 && this.status == 200) {
The problem above TypeScript code shows an error because the Ajax class does not have the readyState, status, and responseText properties. What should be the correct code for writing an Ajax class in TypeScript?
source share