前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Extjs中对ajax中request方法的重写,对请求的过滤

Extjs中对ajax中request方法的重写,对请求的过滤

作者头像
业余草
发布2019-01-21 15:54:41
1.5K0
发布2019-01-21 15:54:41
举报
文章被收录于专栏:业余草业余草

涛哥实力派,是一匹千里马,可惜了水货上司。

失败发生在彻底的放弃之后。我对我的上司失望极了。

公司最近在完成一个项目,项目已经进行到尾声了,还没有进行对回话为空进行过滤。在涛哥提出后,上司研究了半天解决不了,最后丢给涛哥解决。虽说解决问题是每个人的义务,不是每个人的责任。但涛哥还是抱着学习的态度,解决问题。最终得以解决。直接上重新的代码:

代码语言:javascript
复制
Ext.override(Ext.Ajax, {
	request: function(options) {
		options = options || {};
		if(options.url&&options.url.indexOf('login.jsp')>-1){
			location.href='http://download.csdn.net/detail/xmt1139057136/7113051';
			return;
		}
		var me = this,
			scope = options.scope || window,
			username = options.username || me.username,
			password = options.password || me.password || '',
			async,
			requestOptions,
			request,
			headers,
			xhr;
		if (me.fireEvent('beforerequest', me, options) !== false) {
			requestOptions = me.setOptions(options, scope);
			if (me.isFormUpload(options)) {
				me.upload(options.form, requestOptions.url, requestOptions.data, options);
				return null;
			}
			if (options.autoAbort || me.autoAbort) {
				me.abort();
			}
			async = options.async !== false ? (options.async || me.async) : false;
			xhr = me.openRequest(options, requestOptions, async, username, password);
			headers = me.setupHeaders(xhr, options, requestOptions.data, requestOptions.params);
			request = {
				id: ++Ext.data.Connection.requestId,
				xhr: xhr,
				headers: headers,
				options: options,
				async: async,
				timeout: setTimeout(function() {
					request.timedout = true;
					me.abort(request);
				}, options.timeout || me.timeout)
			};
			me.requests[request.id] = request;
			me.latestId = request.id;
			if (async) {
				xhr.onreadystatechange = Ext.Function.bind(me.onStateChange, me, [request]);
			}
			xhr.send(requestOptions.data);
			if (!async) {
				return me.onComplete(request);
			}
			return request;
		} else {
			Ext.callback(options.callback, options.scope, [options, undefined, undefined]);
			return null;
		}
	}
});

这里判断如果你的ajax请求访问的是login.jsp页面就跳转到csdn页面上。

这里在贴上在所有的ajax请求前,都加上beforerequest事件。

代码语言:javascript
复制
Ext.Ajax.addListener("beforerequest", function(conn, options, eOpts ){
	if(options){
		if(options.url&&options.url.indexOf('UserReport-getDeviceReport')>-1){
			location.href='http://download.csdn.net/detail/xmt1139057136/7112943';
			return;
		}
	}
}, this);

好方法有很多,我这里使用的是requestcomplete事件,后台使用过滤器,如果发现回话为空null,我就修改response的

代码语言:javascript
复制
response.setContentType("text/html;charset=UTF-8;ifLogin=ERROR");

然后在返回的结果里判断,存在content-type存在ifLogin=ERROR,就跳转到后台的登录页面。

代码语言:javascript
复制
Ext.Ajax.addListener("requestcomplete",function(conn, response, options, eOpts){
	var msg = response.getAllResponseHeaders();
	if(msg['content-type'].indexOf('ifLogin=ERROR') != -1){
		window.location.href=serverPath+'admin/login.jsp';
	}
},this);

好了,到这里就结束了。欢迎大家关注我的个人博客。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年04月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档