In Typescript I am trying to read an XML file with some javascript code and add the text content to a local variable with the following code:
import { Injectable } from '@angular/core';
@Injectable()
export class JsonreaderService {
private jsonText: string;
constructor() {
this.readTextFile("../../assets/content.json");
}
readTextFile(file)
{
let jsonText: string;
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
this.jsonText = rawFile.responseText;
}
}
}
rawFile.send(null);
}
}
I am trying to assign the text within the json file to my private local variable jsonText: this.jsonText = rawFile.responseText;
which are both of the type 'string'.
When I try this I get the error:
Property 'jsonText' does not exist on type 'XMLHttpRequest'
Does somebody know how I can do this within typescript? When I remove this.
- the responseText can be defined to the jsonText variable which is within the function readTextFile(file)