Home > Web Front-end > JS Tutorial > jquery rewrites ajax and uses the load method to report errors after submitting and judging permissions_jquery

jquery rewrites ajax and uses the load method to report errors after submitting and judging permissions_jquery

WBOY
Release: 2016-05-16 15:19:26
Original
1238 people have browsed it

No more nonsense, let me just post the code for you.

jQuery(function ($) {
// 备份jquery的ajax方法 
var _ajax = $.ajax;
// 重写ajax方法,先判断登录在执行success函数 
$.ajax = function (opt) {
var _success = opt && opt.success || function (a, b) { };
var _opt = $.extend(opt, {
success: function (data, textStatus) {
// 如果后台将请求重定向到了登录页,则data里面存放的就是登录页的源码,这里需要找到data是登录页的证据(标记) 
if ((typeof data) == 'string' && data.indexOf('shangjiaAjaxExtend') != -1) {
window.location.href = 'http://' + window.location.host + '/S/BusiLogin/Index';
return;
} else {
_success(data, textStatus);
}
}
});
return _ajax(_opt);
};
});
Copy after login

The rewriting principle is: since the closure function is loaded before the script on the page, the $.ajax method can be directly rewritten here.

The key point is that the content in the red part must be retrun, otherwise an error "Uncaught TypeError: Cannot call method 'done' of undefined." will be reported when using the load method to load the page.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template