How AJAX controls page jumps in front-end separation scenarios

Source: Internet
Author: User
Tags extend

In the actual application, the front-end separation is more and more common, the frontend is responsible for the routing between the pages, the need for data, send AJAX requests can be. But I hope that when using AJAX, you can control the page jump, how to do?
There are two kinds of situations
In the first case, send a synchronous request, and after successful return, use Window.locatin.href in the callback function
In the second case, return a status code that can be customized 700
Then extend the Ajax code and jump in the error function

In the second case, the scenario is to send Ajax, background judgment session expired, return 700 status code, the foreground can be implemented to jump to the login page.

JQuery (function ($) {
    var _ajax = $.ajax;
    $.ajax = function (opt) {
        var _error = opt && Opt.error | | function (A, B, c) {};
        var _opt = $.extend (opt, {
            error:function (XHR, status, error) {
                if (xhr.status = =) {
                      //Jump to login page
                      TOP.L Ocation.href = "/home/login";
                       return;
                    }
                    _error (XHR, status, error);
                }
        );

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