Merge pull request #2487 from NativeScript/xhr-text-fix

Add toString() method on response with type is text
This commit is contained in:
Alexander Vakrilov
2016-07-21 17:08:16 +03:00
committed by GitHub
3 changed files with 34 additions and 22 deletions

View File

@ -20,6 +20,13 @@
*/
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".
* @param value The value which will be checked.

View File

@ -13,6 +13,13 @@ export function isFunction(value: any): boolean {
return typeof value === "function";
}
export function isObject(value: any): boolean {
if (!value) {
return false;
}
return typeof value === "object";
}
export function isUndefined(value: any): boolean {
return typeof value === "undefined";
}

View File

@ -104,23 +104,20 @@ export class XMLHttpRequest {
this._setResponseType();
if (this.responseType === XMLHttpRequestResponseType.json) {
this._prepareJsonResponse(r);
} else if (this.responseType === XMLHttpRequestResponseType.empty ||
this.responseType === XMLHttpRequestResponseType.text) {
this._responseTextReader = () => r.content.toString();
this._addToStringOnResponse();
if (this.responseType === XMLHttpRequestResponseType.json) {
this._response = JSON.parse(this.responseText);
}
this._setReadyState(this.DONE);
}
private _prepareJsonResponse(r) {
this._responseTextReader = () => r.content.toString();
this._response = JSON.parse(this.responseText);
private _addToStringOnResponse() {
// Add toString() method to ease debugging and
// make Angular2 response.text() method work properly.
if (types.isObject(this.response)) {
Object.defineProperty(this._response, "toString", {
configurable: true,
enumerable: false,
@ -128,6 +125,7 @@ export class XMLHttpRequest {
value: () => this.responseText
});
}
}
private _setResponseType() {
const header = this.getResponseHeader('Content-Type');
@ -180,9 +178,9 @@ export class XMLHttpRequest {
return "";
}
var result = "";
let result = "";
for (var i in this._headers) {
for (let i in this._headers) {
// Cookie headers are excluded
if (i !== "set-cookie" && i !== "set-cookie2") {
result += i + ": " + this._headers[i] + "\r\n";
@ -197,7 +195,7 @@ export class XMLHttpRequest {
&& !this._errorFlag
) {
header = header.toLowerCase();
for (var i in this._headers) {
for (let i in this._headers) {
if (i.toLowerCase() === header) {
return this._headers[i];
}
@ -275,7 +273,7 @@ export class XMLHttpRequest {
}
}
var statuses = {
const statuses = {
100: "Continue",
101: "Switching Protocols",
200: "OK",
@ -330,7 +328,7 @@ export class FormData {
}
toString(): string {
var arr = new Array<string>();
let arr = new Array<string>();
this._data.forEach(function (value, name, map) {
arr.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);