From 5805759c264adf96e9feea78a3cd3bfc2871b1de Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Fri, 18 Nov 2016 12:50:12 +0800 Subject: webui: Redirect to login page if not authenticated on POST request * Remove the authentication requirement on GET operation * "main.js": Redirect to the login page if POST response 403 forbidden * "main.js": Use global AJAX events handling instead of `$.ajaxSetup()` --- fg21sim/webui/static/js/main.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'fg21sim/webui/static/js') diff --git a/fg21sim/webui/static/js/main.js b/fg21sim/webui/static/js/main.js index 27638a7..64b65c1 100644 --- a/fg21sim/webui/static/js/main.js +++ b/fg21sim/webui/static/js/main.js @@ -9,12 +9,19 @@ /** - * jQuery settings + * jQuery AJAX global callbacks using the global AJAX event handler methods + * + * NOTE: + * It is NOT recommended to use `jQuery.ajaxSetup` which will affect ALL calls + * to `jQuery.ajax` or AJAX-based derivatives. */ -jQuery.ajaxSetup({ - error: function (error) { - console.error("AJAX request failed: code:", error.status, - ", reason:", error.statusText); } +$(document).ajaxError(function (event, jqxhr, settings, exception) { + console.error("AJAX request failed: code:", jqxhr.status, + ", reason:", jqxhr.statusText); + if (jqxhr.status === 403) { + // Forbidden error: redirect to login page + window.location.href = "/login"; + } }); @@ -37,6 +44,7 @@ var getCookie = function (name) { return m ? m[1] : undefined; }; + /** * jQuery extension for easier AJAX JSON post * -- cgit v1.2.2