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