Extjs中對ajax中request方法的重寫,對請求的過濾
濤哥實力派,是一匹千裏馬,可惜了水貨上司。
失敗發生在徹底的放棄之後。我對我的上司失望極了。
公司最近在完成一個項目,項目已經進行到尾聲了,還沒有進行對回話為空進行過濾。在濤哥提出後,上司研究了半天解決不了,最後丟給濤哥解決。雖說解決問題是每個人的義務,不是每個人的責任。但濤哥還是抱著學習的態度,解決問題。最終得以解決。直接上重新的代碼:
Ext.override(Ext.Ajax, { request: function(options) { options = options || {}; if(options.url&&options.url.indexOf('login.jsp')>-1){ location.href='https://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='https://download.csdn.net/detail/xmt1139057136/7112943'; return; } } }, this);
好方法有很多,我這裏使用的是requestcomplete事件,後台使用過濾器,如果發現回話為空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);好了,到這裏就結束了。歡迎大家關注我的個人博客。
最後更新:2017-04-03 12:56:08