重写ajax方法

allaniori 2019-04-16 10:55:28
我想重写ajax方法,想在重写方法里统一处理权限和异常,百度了一圈,也封装了,但是发现个问题,没封装前的方法可以设置headers,但是封装后就不识别了,有没有大神指点一下,想要封装ajax方法,header怎么处理
重写的如下
(function($){
//首先备份下jquery的ajax方法
var _ajax=$.ajax;

//重写jquery的ajax方法
$.ajax = function (opt) {

//备份opt中error和success方法
opt.type = 'POST';
opt.cache = false;
alert( opt.headers)
var fn = {
error:function(XMLHttpRequest, textStatus, errorThrown){},
success:function(data, textStatus){}
}
if(opt.error){
fn.error=opt.error;
}
if(opt.success){
fn.success=opt.success;
}
。。。。。。

没封装的时候
$.ajax({

headers: headers,
url: url,
data: {
userid: userid,
lockstate: lockstate
},
。。。。。。
这个headers怎么办,里面是token
...全文
212 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
2019-04-19
  • 打赏
  • 举报
回复

$(function(){
	var _ajax = $.ajax;
	$.ajax = function(opt){
		opt = $.extend({
			type: 'POST',
			cache: false,
			//其它默认值
		}, opt);

		_ajax(opt);
	};
});

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧