Use jQuery Ajax to filter out uninterrupted REST sessions
Use jQuery Ajax to filter out uninterrupted REST sessions
When a client-server session times out and an Ajax data request is sent, what should I do if I encounter a 403 status code?
In the Shell terminal, when the super administrator session times out, the password is automatically displayed for confirmation. For the page, is it necessary to capture failure requests with a status code of 403, replay one by one after successful login, or directly refresh the page after successful login?
JQuery has evolved to provide the ability to process such businesses. I found an article about madpilot rants, and all these problems were solved.
The specific process is:
When the request encounters a 403 error, it will be placed in the 403 queue and the login box will pop up. When the login is successful, it will automatically resend the unsuccessful request in the queue.
However, this solution currently has some drawbacks: it does not support the use of Promise style to add success callbacks. success callbacks must be written in options, that is:
// Supports jQuery. ajax ("/foo. json ", {success: function (data) {// TODO}); // jQuery is not supported. ajax ("/foo. json "). done (function (data) {// TODO });
The following schemes refer to the rants scheme and take into account the 403 failures of all concurrent requests:
Require (["jquery"], function ($) {// Thanks @ madpilot rants // See the http://myles.eftos.id.au/blog/2011/11/30/how-to-re-play-an-ajax-request-in-jquery-after-an-authentication-error/#.U-iHifmSx8Evar isDialogShown = false; var ajaxQueue = []; jQuery. ajaxSetup ({statusCode: {// when the session is not logged on or fails after login, the server returns the status code 403403: function () {ajaxQueue. push (this); if (isDialogShown) {return;} isDialogShown = true; require (["infrastructure/user/LoginDialog"], function (LoginDialog) {var dialog = new LoginDialog ("loginDialog", {onfullfilled: function () {isDialogShown = false; ajaxQueue. forEach (function (context) {$. ajax (context) ;}); ajaxQueue. length = 0 ;}, onrejected: function () {}}); dialog. placeAt (document. body, "beforeEnd") dialog. open ();});}}});});