Skip to content

Commit

Permalink
fix(ajax): response properly based off responseType
Browse files Browse the repository at this point in the history
@trxcllnt discovered an issue with accessing both response and responseText in Chrome if responseType was set
I copy-pastad his code in the name of speed with his blessing. I <3 Paul
  • Loading branch information
benlesh committed Jan 13, 2016
1 parent 34ee933 commit b2a27a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion spec/observables/dom/ajax-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Observable.ajax', function () {
url: '/flibbertyJibbet',
responseType: 'text',
resultSelector: function (res) {
return res.responseText;
return res.response;
}
})
.subscribe(function(x) {
Expand Down
39 changes: 19 additions & 20 deletions src/observable/dom/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function ajaxPut<T>(url: string, body?: any, headers?: Object): Observabl
};

export function ajaxGetJSON<T, R>(url: string, resultSelector?: (data: T) => R, headers?: Object): Observable<R> {
const finalResultSelector = resultSelector ? (res: AjaxResponse) => resultSelector(res.response) : null;
const finalResultSelector = resultSelector ? (res: AjaxResponse) => resultSelector(res.response) : (res: AjaxResponse) => res.response;
return new AjaxObservable<R>({ method: 'GET', url, responseType: 'json', resultSelector: finalResultSelector, headers });
};
/**
Expand Down Expand Up @@ -336,35 +336,34 @@ export class AjaxResponse {
/** {number} the HTTP status code */
status: number;

/** {string|ArrayBuffer|object|any} the response data */
/** {string|ArrayBuffer|Document|object|any} the response data */
response: any;

/** {string} the raw responseText */
responseText: string;

/** {string} the responsType (e.g. 'json' or 'array-buffer') */
/** {string} the responsType (e.g. 'json', 'arraybuffer', or 'xml') */
responseType: string;

/** {Document} an XML Document from the response */
responseXML: Document;

constructor(public originalEvent: Event, public xhr: XMLHttpRequest, public request: AjaxRequest) {
this.status = xhr.status;
const responseType = xhr.responseType;
let response: any;
if ('response' in xhr) {
response = xhr.response;
} else {
if (request.responseType === 'json') {
response = JSON.parse(xhr.responseText);
} else {
response = xhr.responseText;
}
this.responseType = xhr.responseType;
switch (this.responseType) {
case 'json':
if ('response' in xhr) {
this.response = xhr.response;
} else {
this.response = JSON.parse(xhr.responseText || '');
}
break;
case 'xml':
this.response = xhr.responseXML;
break;
case 'text':
default:
this.response = ('response' in xhr) ? xhr.response : xhr.responseText;
break;
}
this.responseText = xhr.responseText;
this.responseType = responseType;
this.responseXML = xhr.responseXML;
this.response = response;
}
}

Expand Down

0 comments on commit b2a27a2

Please sign in to comment.