mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
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:
@ -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");
|
||||||
|
@ -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;
|
||||||
|
Reference in New Issue
Block a user