1. 程式人生 > >Extjs中對ajax中request方法的重寫,對請求的過濾

Extjs中對ajax中request方法的重寫,對請求的過濾

濤哥實力派,是一匹千里馬,可惜了水貨上司。

失敗發生在徹底的放棄之後。我對我的上司失望極了。

公司最近在完成一個專案,專案已經進行到尾聲了,還沒有進行對回話為空進行過濾。在濤哥提出後,上司研究了半天解決不了,最後丟給濤哥解決。雖說解決問題是每個人的義務,不是每個人的責任。但濤哥還是抱著學習的態度,解決問題。最終得以解決。直接上重新的程式碼:

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事件

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

好方法有很多,我這裡使用的是事件,後臺使用過濾器,如果發現回話為空null,我就修改response

response.setContentType("text/html;charset=UTF-8;ifLogin=ERROR");
然後在返回的結果裡判斷,存在content-type存在ifLogin=ERROR,就跳轉到後臺的登入頁面。
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);
好了,到這裡就結束了。歡迎大家關注我的個人部落格。