From d629c7aef82129f810bd46afd683cc1bef9cfdd4 Mon Sep 17 00:00:00 2001 From: Isiah Meadows Date: Wed, 3 Jul 2019 12:43:38 -0400 Subject: [PATCH] Fix #2442 (#2449) * Fix #2442 * Add PR link --- docs/change-log.md | 1 + request/request.js | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/docs/change-log.md b/docs/change-log.md index 4b696c28..4582b2d9 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -99,6 +99,7 @@ - render: fix when attrs change with `onbeforeupdate` returning false, then remaining the same on next redraw ([#2447](https://github.com/MithrilJS/mithril.js/pull/2447) [@isiahmeadows](https://github.com/isiahmeadows)) - render: fix internal error when `onbeforeupdate` returns false and then true with new child tree ([#2447](https://github.com/MithrilJS/mithril.js/pull/2447) [@isiahmeadows](https://github.com/isiahmeadows)) - route: arbitrary prefixes are properly supported now, including odd prefixes like `?#` and invalid prefixes like `#foo#bar` ([#2448](https://github.com/MithrilJS/mithril.js/pull/2448) [@isiahmeadows](https://github.com/isiahmeadows)) +- request: correct IE workaround for response type non-support ([#2449](https://github.com/MithrilJS/mithril.js/pull/2449) [@isiahmeadows](https://github.com/isiahmeadows)) --- diff --git a/request/request.js b/request/request.js index 55057b8b..b0faaaa7 100644 --- a/request/request.js +++ b/request/request.js @@ -77,6 +77,7 @@ module.exports = function($window, Promise) { var method = args.method != null ? args.method.toUpperCase() : "GET" var body = args.body var assumeJSON = (args.serialize == null || args.serialize === JSON.serialize) && !(body instanceof $window.FormData) + var responseType = args.responseType || (typeof args.extract === "function" ? "" : "json") var xhr = new $window.XMLHttpRequest(), aborted = false, @@ -97,7 +98,7 @@ module.exports = function($window, Promise) { } if (args.withCredentials) xhr.withCredentials = args.withCredentials if (args.timeout) xhr.timeout = args.timeout - xhr.responseType = args.responseType || (typeof args.extract === "function" ? "" : "json") + xhr.responseType = responseType for (var key in args.headers) { if ({}.hasOwnProperty.call(args.headers, key)) { @@ -121,18 +122,17 @@ module.exports = function($window, Promise) { // preferring `xhr.response` where possible/practical. var response = xhr.response, message - if (response == null) { - try { - response = xhr.responseText - // Note: this snippet is intentionally *after* - // `xhr.responseText` is accessed, since the - // above will throw in modern browsers (thus - // skipping the rest of this section). It's an - // IE hack to detect and work around the lack of - // native `responseType: "json"` support there. - if (typeof args.extract !== "function" && xhr.responseType === "json") response = JSON.parse(response) - } - catch (e) { response = null } + if (responseType === "json") { + // For IE and Edge, which don't implement + // `responseType: "json"`. + if (!xhr.responseType && typeof args.extract !== "function") response = JSON.parse(xhr.responseText) + } else if (!responseType || responseType === "text") { + // Only use this default if it's text. If a parsed + // document is needed on old IE and friends (all + // unsupported), the user should use a custom + // `config` instead. They're already using this at + // their own risk. + if (response == null) response = xhr.responseText } if (typeof args.extract === "function") {