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

來源:互聯網
上載者:User

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

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

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

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

好方法有很多,我這裡使用的是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);
好了,到這裡就結束了。歡迎大家關注我的個人部落格。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.