Attempting to read an XML file in Typescript using some JavaScript code, with the goal of adding the text content to a local variable. The code snippet is as follows:
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);
}
}
Effort to assign text from the JSON file to private local variable jsonText - both being of type 'string':
this.jsonText = rawFile.responseText;
Encountering an error when trying:
Property 'jsonText' does not exist on type 'XMLHttpRequest'
Would appreciate any insight on executing this task in TypeScript. Omitting this.
allows for defining responseText to the jsonText variable within the function readTextFile(file)
.