diff --git a/xhr/xhr.ts b/xhr/xhr.ts index 9a4fa0951..de341b181 100644 --- a/xhr/xhr.ts +++ b/xhr/xhr.ts @@ -105,8 +105,8 @@ export class XMLHttpRequest { this._setResponseType(); if (this.responseType === XMLHttpRequestResponseType.json) { - this._responseTextReader = () => r.content.toString(); - this._response = JSON.parse(this.responseText); + this._prepareJsonResponse(r); + } else if (this.responseType === XMLHttpRequestResponseType.empty || this.responseType === XMLHttpRequestResponseType.text) { this._responseTextReader = () => r.content.toString(); @@ -115,6 +115,20 @@ export class XMLHttpRequest { this._setReadyState(this.DONE); } + private _prepareJsonResponse(r) { + this._responseTextReader = () => r.content.toString(); + this._response = JSON.parse(this.responseText); + + // Add toString() method to ease debugging and + // make Angular2 response.text() method work properly. + Object.defineProperty(this._response, "toString", { + configurable: true, + enumerable: false, + writable: true, + value: () => this.responseText + }); + } + private _setResponseType() { const header = this.getResponseHeader('Content-Type'); const contentType = header && header.toLowerCase();