Use jQuery Ajax to filter out uninterrupted REST sessions

Source: Internet
Author: User

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 ();});}}});});


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.