mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge pull request #1984 from NativeScript/feature/xhr-ng2-text
Attach toString() method to xhr response
This commit is contained in:
18
xhr/xhr.ts
18
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();
|
||||
|
||||
Reference in New Issue
Block a user