The session expired processing scenario when the AJAX request was made

Source: Internet
Author: User

The web system often uses interceptors or filters to implement permission interception, to determine whether the user login, if not logged, jump to the landing page, but when the browser is the AJAX request, the browser will not jump normally, but return to the landing page of the HTML source to the Ajax callback function (when the Jump landing page cross-domain, 302 errors will occur), the solution is as follows: Front end://If the session is timed out or not logged in, jump to the landing page
$ (document). Ajaxcomplete (function(event,request, settings) {  var data=  Request.responsejson;   if (data.ret!=null&&data.ret==302) // Judging by the data returned by the server side   {           window.location=data.redirecturl;  }});

Server:
@RequestMapping (value = "/nologin") public void Login (httpservletrequest request,httpservletresponse response, Model Model) throws Exception {  if (Request.getheader ("X-requested-with")!=null&&request.getheader (" X-requested-with "). Equals (" XMLHttpRequest "))  {   response.getwriter (). Write ({" ret ": 302," MSG ":"  "," RedirectURL ":"} ");  else {   Response.sendredirect (applicationprops.getproperty ("Oauth.ucenter.url"));}  }

The server depends on whether the request header X-requested-with has a value and whether it is XMLHttpRequest (which represents an AJAX request) to determine if it is an AJAX request, and if so, to return the JSON data, if not the normal jump; The front end is the global complete method added to the AJAX request, which is triggered when the AJAX request completes (regardless of the success of the request), where Var Data=request.responsejson; is the Jason data returned by the server, which completes the jump based on the JSON data.

The session expired processing scenario when the AJAX request was made

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.