Skip to content

Commit 66ff58e

Browse files
author
Alexander Vakrilov
authored
Merge pull request NativeScript#2487 from NativeScript/xhr-text-fix
Add toString() method on response with type is text
2 parents d2f34a9 + 2cdd1f2 commit 66ff58e

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

tns-core-modules/utils/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
* Returns true if value is a function.
2020
*/
2121
export function isFunction(value: any): boolean;
22+
23+
/**
24+
* A function that checks if something is an object.
25+
* @param value The value which will be checked.
26+
* Returns true if value is an object.
27+
*/
28+
export function isObject(value: any): boolean;
2229

2330
/**
2431
* A function that checks if something is "undefined".

tns-core-modules/utils/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export function isFunction(value: any): boolean {
1313
return typeof value === "function";
1414
}
1515

16+
export function isObject(value: any): boolean {
17+
if (!value) {
18+
return false;
19+
}
20+
return typeof value === "object";
21+
}
22+
1623
export function isUndefined(value: any): boolean {
1724
return typeof value === "undefined";
1825
}

tns-core-modules/xhr/xhr.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class XMLHttpRequest {
8181
this._options.content = (<FormData>data).toString();
8282
}
8383

84-
http.request(this._options).then(r=> {
84+
http.request(this._options).then(r => {
8585
if (!this._errorFlag) {
8686
this._loadResponse(r);
8787
}
@@ -104,29 +104,27 @@ export class XMLHttpRequest {
104104

105105
this._setResponseType();
106106

107-
if (this.responseType === XMLHttpRequestResponseType.json) {
108-
this._prepareJsonResponse(r);
107+
this._responseTextReader = () => r.content.toString();
108+
this._addToStringOnResponse();
109109

110-
} else if (this.responseType === XMLHttpRequestResponseType.empty ||
111-
this.responseType === XMLHttpRequestResponseType.text) {
112-
this._responseTextReader = () => r.content.toString();
110+
if (this.responseType === XMLHttpRequestResponseType.json) {
111+
this._response = JSON.parse(this.responseText);
113112
}
114113

115114
this._setReadyState(this.DONE);
116115
}
117116

118-
private _prepareJsonResponse(r) {
119-
this._responseTextReader = () => r.content.toString();
120-
this._response = JSON.parse(this.responseText);
121-
117+
private _addToStringOnResponse() {
122118
// Add toString() method to ease debugging and
123119
// make Angular2 response.text() method work properly.
124-
Object.defineProperty(this._response, "toString", {
125-
configurable: true,
126-
enumerable: false,
127-
writable: true,
128-
value: () => this.responseText
129-
});
120+
if (types.isObject(this.response)) {
121+
Object.defineProperty(this._response, "toString", {
122+
configurable: true,
123+
enumerable: false,
124+
writable: true,
125+
value: () => this.responseText
126+
});
127+
}
130128
}
131129

132130
private _setResponseType() {
@@ -180,9 +178,9 @@ export class XMLHttpRequest {
180178
return "";
181179
}
182180

183-
var result = "";
181+
let result = "";
184182

185-
for (var i in this._headers) {
183+
for (let i in this._headers) {
186184
// Cookie headers are excluded
187185
if (i !== "set-cookie" && i !== "set-cookie2") {
188186
result += i + ": " + this._headers[i] + "\r\n";
@@ -197,7 +195,7 @@ export class XMLHttpRequest {
197195
&& !this._errorFlag
198196
) {
199197
header = header.toLowerCase();
200-
for (var i in this._headers) {
198+
for (let i in this._headers) {
201199
if (i.toLowerCase() === header) {
202200
return this._headers[i];
203201
}
@@ -275,7 +273,7 @@ export class XMLHttpRequest {
275273
}
276274
}
277275

278-
var statuses = {
276+
const statuses = {
279277
100: "Continue",
280278
101: "Switching Protocols",
281279
200: "OK",
@@ -330,9 +328,9 @@ export class FormData {
330328
}
331329

332330
toString(): string {
333-
var arr = new Array<string>();
331+
let arr = new Array<string>();
334332

335-
this._data.forEach(function(value, name, map) {
333+
this._data.forEach(function (value, name, map) {
336334
arr.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
337335
});
338336

0 commit comments

Comments
 (0)