mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +08:00
Add toString() method on response whtn type is text
This commit is contained in:
7
tns-core-modules/utils/types.d.ts
vendored
7
tns-core-modules/utils/types.d.ts
vendored
@ -20,6 +20,13 @@
|
|||||||
*/
|
*/
|
||||||
export function isFunction(value: any): boolean;
|
export function isFunction(value: any): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function that checks if something is an object.
|
||||||
|
* @param value The value which will be checked.
|
||||||
|
* Returns true if value is an object.
|
||||||
|
*/
|
||||||
|
export function isObject(value: any): boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A function that checks if something is "undefined".
|
* A function that checks if something is "undefined".
|
||||||
* @param value The value which will be checked.
|
* @param value The value which will be checked.
|
||||||
|
@ -13,6 +13,13 @@ export function isFunction(value: any): boolean {
|
|||||||
return typeof value === "function";
|
return typeof value === "function";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isObject(value: any): boolean {
|
||||||
|
if (!value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return typeof value === "object";
|
||||||
|
}
|
||||||
|
|
||||||
export function isUndefined(value: any): boolean {
|
export function isUndefined(value: any): boolean {
|
||||||
return typeof value === "undefined";
|
return typeof value === "undefined";
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ export class XMLHttpRequest {
|
|||||||
this._options.content = (<FormData>data).toString();
|
this._options.content = (<FormData>data).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
http.request(this._options).then(r=> {
|
http.request(this._options).then(r => {
|
||||||
if (!this._errorFlag) {
|
if (!this._errorFlag) {
|
||||||
this._loadResponse(r);
|
this._loadResponse(r);
|
||||||
}
|
}
|
||||||
@ -104,29 +104,27 @@ export class XMLHttpRequest {
|
|||||||
|
|
||||||
this._setResponseType();
|
this._setResponseType();
|
||||||
|
|
||||||
if (this.responseType === XMLHttpRequestResponseType.json) {
|
this._responseTextReader = () => r.content.toString();
|
||||||
this._prepareJsonResponse(r);
|
this._addToStringOnResponse();
|
||||||
|
|
||||||
} else if (this.responseType === XMLHttpRequestResponseType.empty ||
|
if (this.responseType === XMLHttpRequestResponseType.json) {
|
||||||
this.responseType === XMLHttpRequestResponseType.text) {
|
this._response = JSON.parse(this.responseText);
|
||||||
this._responseTextReader = () => r.content.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._setReadyState(this.DONE);
|
this._setReadyState(this.DONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _prepareJsonResponse(r) {
|
private _addToStringOnResponse() {
|
||||||
this._responseTextReader = () => r.content.toString();
|
|
||||||
this._response = JSON.parse(this.responseText);
|
|
||||||
|
|
||||||
// Add toString() method to ease debugging and
|
// Add toString() method to ease debugging and
|
||||||
// make Angular2 response.text() method work properly.
|
// make Angular2 response.text() method work properly.
|
||||||
Object.defineProperty(this._response, "toString", {
|
if (types.isObject(this.response)) {
|
||||||
configurable: true,
|
Object.defineProperty(this._response, "toString", {
|
||||||
enumerable: false,
|
configurable: true,
|
||||||
writable: true,
|
enumerable: false,
|
||||||
value: () => this.responseText
|
writable: true,
|
||||||
});
|
value: () => this.responseText
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _setResponseType() {
|
private _setResponseType() {
|
||||||
@ -180,9 +178,9 @@ export class XMLHttpRequest {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = "";
|
let result = "";
|
||||||
|
|
||||||
for (var i in this._headers) {
|
for (let i in this._headers) {
|
||||||
// Cookie headers are excluded
|
// Cookie headers are excluded
|
||||||
if (i !== "set-cookie" && i !== "set-cookie2") {
|
if (i !== "set-cookie" && i !== "set-cookie2") {
|
||||||
result += i + ": " + this._headers[i] + "\r\n";
|
result += i + ": " + this._headers[i] + "\r\n";
|
||||||
@ -197,7 +195,7 @@ export class XMLHttpRequest {
|
|||||||
&& !this._errorFlag
|
&& !this._errorFlag
|
||||||
) {
|
) {
|
||||||
header = header.toLowerCase();
|
header = header.toLowerCase();
|
||||||
for (var i in this._headers) {
|
for (let i in this._headers) {
|
||||||
if (i.toLowerCase() === header) {
|
if (i.toLowerCase() === header) {
|
||||||
return this._headers[i];
|
return this._headers[i];
|
||||||
}
|
}
|
||||||
@ -275,7 +273,7 @@ export class XMLHttpRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var statuses = {
|
const statuses = {
|
||||||
100: "Continue",
|
100: "Continue",
|
||||||
101: "Switching Protocols",
|
101: "Switching Protocols",
|
||||||
200: "OK",
|
200: "OK",
|
||||||
@ -330,9 +328,9 @@ export class FormData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toString(): string {
|
toString(): string {
|
||||||
var arr = new Array<string>();
|
let arr = new Array<string>();
|
||||||
|
|
||||||
this._data.forEach(function(value, name, map) {
|
this._data.forEach(function (value, name, map) {
|
||||||
arr.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
|
arr.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user