NativeScript/NativeScript · error · Error
Failed to read the 'responseText' property from 'XMLHttpRequ
Error message
Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'text' (was '${this._responseType}'). What it means
Per the XHR spec, responseText is only readable when responseType is '' (empty) or 'text'. This XHR implementation enforces that: reading responseText after setting responseType to 'json', 'arraybuffer', or 'blob' throws this descriptive error.
Source
Thrown at packages/core/xhr/index.ts:65
public get readyState(): number {
return this._readyState;
}
public get responseType(): string {
return this._responseType;
}
public set responseType(value: string) {
if (value === XMLHttpRequestResponseType.empty || value in XMLHttpRequestResponseType) {
this._responseType = value;
} else {
throw new Error(`Response type of '${value}' not supported.`);
}
}
public get responseText(): string {
if (this._responseType !== XMLHttpRequestResponseType.empty && this._responseType !== XMLHttpRequestResponseType.text) {
throw new Error("Failed to read the 'responseText' property from 'XMLHttpRequest': " + "The value is only accessible if the object's 'responseType' is '' or 'text' " + `(was '${this._responseType}').`);
}
if (isFunction(this._responseTextReader)) {
return this._responseTextReader();
}
return '';
}
public get response(): any {
if (this._responseType === XMLHttpRequestResponseType.empty || this._responseType === XMLHttpRequestResponseType.text) {
if (this._readyState !== this.LOADING && this._readyState !== this.DONE) {
return '';
} else {
return this._response;
}
} else {
if (this._readyState !== this.DONE) {View on GitHub (pinned to 6800aefa65)
Solutions
- Read `xhr.response` instead of `xhr.responseText` when responseType is not '' or 'text'.
- Reset `xhr.responseType = ''` (or 'text') before reading responseText — requires a new request for some implementations, so prefer reading response.
- Branch on responseType in your load handler: use responseText for text modes, response otherwise.
Example fix
// before xhr.responseType = 'json'; xhr.onload = () => console.log(xhr.responseText); // throws // after xhr.responseType = 'json'; xhr.onload = () => console.log(xhr.response);
Defensive patterns
Strategy: validation
Validate before calling
const body = xhr.responseType === '' || xhr.responseType === 'text' ? xhr.responseText : xhr.response;
Type guard
function isTextResponseType(rt: string): boolean {
return rt === '' || rt === 'text';
} Try / catch
let body: any;
try {
body = xhr.responseText;
} catch (e) {
if (/responseText/.test(e.message)) { body = xhr.response; } else { throw e; }
} Prevention
- Read xhr.response unless responseType is '' or 'text'.
- Branch on responseType in onload handlers.
- Avoid mixing legacy responseText reads with typed responseType requests.
When it happens
Trigger: Setting `xhr.responseType = 'json'` (or 'arraybuffer'/'blob') and then reading `xhr.responseText` instead of `xhr.response`.
Common situations: Mixed code paths where one branch sets responseType for JSON and legacy code still reads responseText; copy-pasted handlers reading responseText regardless of the configured responseType.
Related errors
- Response type of '${value}' not supported.
- Http request must provide a valid url.
- Response content may not be converted to string
- Cannot save file with path: ${destinationFilePath}.
- Response content may not be converted to string
AI-assisted analysis of NativeScript/NativeScript@6800aefa65 (2026-08-30).
Data as JSON: /api/errors/056e620ff6eaf1a1.
Report an issue: GitHub.