Solve the interception instance of the interceptor to Ajax request detailed _jquery

Source: Internet
Author: User

Resolves the interceptor's interception of Ajax requests

Interceptor Configuration:

 public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object obj) throws Exception {//Get the decision to log whether the session exists a String token = (string) request.getsession (). getattribute
    ("token");
    String PostID = (string) request.getsession (). getattribute ("PostID");
      if (token = NULL | | token = = "") {String xrequested =request.getheader ("X-requested-with");
      if ("XMLHttpRequest". Equals (xrequested)) {response.getwriter (). Write ("Isajax");
      }else{response.sendredirect ("/m-web/user/tologin");
    return false;
      } if (PostID = null | | postid = = "") {String xrequested =request.getheader ("X-requested-with");
      if ("XMLHttpRequest". Equals (xrequested)) {response.getwriter (). Write ("Isajax");
      }else{response.sendredirect ("/m-web/user/tologin");
    return false;
  return true; }

1. Determine the value of String xrequested =request.getheader ("X-requested-with") to determine whether it is an AJAX request.

2, Response.getwriter (). Write ("Isajax"), write a response data to Ajax, so you can make judgments in Ajax

There are two ways of judging:

1 directly in the Ajax inside make judgments (not recommended)

Success:function (data) {
  if (data = = "Isajax") {
    window.location.href= "m-web/user/tologin" return
    ;
  }
}

2 to change the Ajax source code and then in the compression, is the overall way to modify (recommended)

if (issuccess) {//If no content
        if (status = = 204 | | | s.type = = "Head") {
          statustext = "nocontent";

        If not modified
        } else if (status = = 304) {
          statustext = "notmodified";

        If We have data, let ' s convert it
        } else {
          statustext = response.state;
          Success = Response.data;
          Error = Response.error;
          issuccess =!error;
          Solve the Ajax interception problem
          var result = Responses.text;
          if (Result.indexof ("Isajax") >=0) {
            window.location.href= "m-web/user/tologin";
            return;}}
      

Thank you for reading, I hope to help you, thank you for your support for this site!

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.