When jquery Ajax encounters a 401-Request solution _jquery

Source: Internet
Author: User

jquery Ajax is a very common interface, and at the time of the request, there may be a response of 401 (identity authentication expired or not logged in), more likely to appear in a mixed application, how to authenticate, the failure to repeat the request, it is worth noting.

Ajax requests have two ways

1. Callback

In the most frequently written way, successful failure processing is passed in as a callback.

$.ajax ({
   ajax parameters ...
   ) Success:xxxxxx
   error:xxxxxx
});

2. Deferred mode

Deferred mode I am in "JS asynchronous Programming" has explained that the AJAX call itself returns is a deferred object, success failure callback is not passed in parameters.

$.ajax ({
   ajax parameters ...
}). Then (function (res) {
  //successfully processing fragment
},function (err) {
  //Failed processing fragment
});

Since there are two ways, there are two ways to deal with 401.

401 two ways to deal with

1. Callback

This way of processing is relatively simple, in the failure of the callback inside the Judge 401, if it is the identity of authentication, the successful request to repeat.

function getxxxx (type, URL, data, success, error) {
$.ajax ({
  ajax parameter ...
  ) Success:xxxxxx
  error:function (xhr,textstatus,errorthrown) {
    if (xhr.status = = 401) {
    Refresh identity authentication method (fun  Ction () {
       getxxxx (type, URL, data, success, error);
      });
     else{
      //Calls external error
      error && error (Xhr,textstatus,errorthrown);
   }
  }
);

2. Deferred mode

The way I find it now is to modify the jquery source.

//Global set a method $.ajaxsetup ({autherror:function (callback) {Refresh authentication method (function () {
      Callback && callback ();
   });
 
}
}); jquery2.1.4 version of the source code, is about 8261 lines//Success/error if (issuccess) {deferred.resolvewith (Callbackcontext, [Success, status
Text, JQXHR]); else {if (Jqxhr.status = = 401 | | jqxhr. Status = = 403) && callbackcontext.autherror) {Callbackcontext.
      Autherror (function () {state = 0;
      Jqxhr.setrequestheader ("Authorization", XXXXXX);
      Jqxhr.readystate = 1;
        try {state = 1;
      Transport.send (requestheaders, done); catch (E) {//Propagate exception as error if not do if (State < 2) {done (-1, E)
        ;
        Simply Rethrow otherwise} else {throw e;
     } }   });
   Return
   else {deferred.rejectwith (callbackcontext, [JQXHR, StatusText, error]); }
}

Here's why you can't make a request like the first one.

There are two reasons:

1. Then this type of chain, resulting in the callback of this request is not in the parameters, but in jquery.callbacks a optionscache global variable, we can not get the callback function in the AJAX error to the postback.

2. A callback written in then will be destroyed once, and when the error is triggered, the callback is executed and destroyed.

The last way to do this is to intercept 401 of errors before triggering the error, re-authenticate, and then reset the status and send the request again.

Above this when the jquery Ajax encounter 401 Request solution is small series to share all the content, hope to give you a reference, also hope that we support cloud habitat community.

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.