luci-base: xhr.js: decode JSON for POST requests as well
authorJo-Philipp Wich <jo@mein.io>
Fri, 27 Jul 2018 11:23:58 +0000 (13:23 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sat, 28 Jul 2018 15:14:22 +0000 (17:14 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit 98217f8f8dd1835824405d5bf3ceb95dd8f40032)

modules/luci-base/htdocs/luci-static/resources/xhr.js

index 62b525ebb03ac465d152b4ad5ff5fc58026fb952..25a90e7254ffbcccb203c55e068b7dc8840ca0b2 100644 (file)
@@ -65,12 +65,8 @@ XHR = function()
                        if (xhr.readyState == 4) {
                                var json = null;
                                if (xhr.getResponseHeader("Content-Type") == "application/json") {
-                                       try {
-                                               json = JSON.parse(xhr.responseText);
-                                       }
-                                       catch(e) {
-                                               json = null;
-                                       }
+                                       try { json = JSON.parse(xhr.responseText); }
+                                       catch(e) { json = null; }
                                }
 
                                callback(xhr, json, Date.now() - ts);
@@ -90,8 +86,15 @@ XHR = function()
 
                xhr.onreadystatechange = function()
                {
-                       if (xhr.readyState == 4)
-                               callback(xhr, null, Date.now() - ts);
+                       if (xhr.readyState == 4) {
+                               var json = null;
+                               if (xhr.getResponseHeader("Content-Type") == "application/json") {
+                                       try { json = JSON.parse(xhr.responseText); }
+                                       catch(e) { json = null; }
+                               }
+
+                               callback(xhr, json, Date.now() - ts);
+                       }
                }
 
                xhr.open('POST', url, true);