Merge pull request #2602 from acramatte/xhr-json-syntax-suffix-support

Parse XHR response content type with +json as JSON
This commit is contained in:
Vladimir Enchev
2016-08-18 09:33:04 +03:00
committed by GitHub
2 changed files with 22 additions and 2 deletions

View File

@ -298,7 +298,27 @@ export function test_xhr_responseType_switched_to_JSON_if_header_present() {
"Content-Type": "application/json" "Content-Type": "application/json"
} }
};
xhr._loadResponse(response);
TKUnit.assertEqual(xhr.responseType, "json");
TKUnit.assertEqual(xhr.response.data, 42);
} }
export function test_xhr_responseType_switched_to_JSON_if_headers_content_type_has_json_suffix() {
const xhr = <any>new XMLHttpRequest();
const response = {
statusCode: 200,
content: {
toString: function () {
return this.raw
},
raw: '{"data": 42}'
},
headers: {
"Content-Type": "type/media.type+json"
}
};
xhr._loadResponse(response); xhr._loadResponse(response);
TKUnit.assertEqual(xhr.responseType, "json"); TKUnit.assertEqual(xhr.responseType, "json");

View File

@ -132,7 +132,7 @@ export class XMLHttpRequest {
const contentType = header && header.toLowerCase(); const contentType = header && header.toLowerCase();
if (contentType) { if (contentType) {
if (contentType.indexOf('application/json') >= 0) { if (contentType.indexOf('application/json') >= 0 || contentType.indexOf('+json') >= 0) {
this.responseType = XMLHttpRequestResponseType.json; this.responseType = XMLHttpRequestResponseType.json;
} else if (contentType.indexOf('text/plain') >= 0) { } else if (contentType.indexOf('text/plain') >= 0) {
this.responseType = XMLHttpRequestResponseType.text; this.responseType = XMLHttpRequestResponseType.text;