Merge pull request #1984 from NativeScript/feature/xhr-ng2-text

Attach toString() method to xhr response
This commit is contained in:
Alexander Vakrilov
2016-04-18 14:10:45 +03:00

View File

@@ -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();