ExtJs 4.x Ajax简单封装
生活随笔
收集整理的這篇文章主要介紹了
ExtJs 4.x Ajax简单封装
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*** Ajax 請求*/
Ext.define("SinoCloudAjaxRequestClass", {constructor : function () {var me = this;var viewport = me.getViewPort();if(viewport){window.sinoCloudAjaxRequestClassLoadingMak = new Ext.LoadMask(viewport, {msg:"處理中..."});}},request : function (method,param, url, func,anotherFunc) { //用于執(zhí)行ajax請求,其中,func為第一個回調(diào)方法,一般用于執(zhí)行成功提示,可以使用該類的commonPrompt,也可以直接處理成功請求后的業(yè)務(wù),anotherFunc用于處理請求成功后執(zhí)行的業(yè)務(wù),可選var me = this;me.showViewportLoading();Ext.Ajax.request({url: url,params: param,method: method,success: function (response, options) {me.hideViewportLoading();if(func){func(response, options,anotherFunc);}else{Ext.Msg.alert('error', 'error...did not define ajax callback function...');}}, failure: function (response, options) {me.hideViewportLoading();Ext.Msg.alert('錯誤', '系統(tǒng)錯誤,請稍候再試!');}});},get: function (param, url, func,anotherFunc) {var me = this;me.request("get",param,url,func,anotherFunc);}, post: function (param, url, func,anotherFunc) {var me = this;me.request("post",param,url,func,anotherFunc);},commonPrompt : function (response, options,anotherFunc) { //一般提示if(anotherFunc){anotherFunc(response,options);}var text = response.responseText;if (text) {text = text.trim();var json = Ext.decode(text);var success = json.success;if (success) {Ext.Msg.alert('提示', "操作成功!");} else {var msg = json.msg;if(msg){Ext.Msg.alert('提示', msg);}else{Ext.Msg.alert('提示', "操作失敗!");}}} else {Ext.Msg.alert('提示', "系統(tǒng)錯誤...");}},getViewPort : function () {return Ext.getBody();},showViewportLoading : function () {if(sinoCloudAjaxRequestClassLoadingMak){sinoCloudAjaxRequestClassLoadingMak.show();}},hideViewportLoading : function () {if(sinoCloudAjaxRequestClassLoadingMak){sinoCloudAjaxRequestClassLoadingMak.hide();}}
});
?
?
使用方式 :?
var ajax = Ext.create("SinoCloudAjaxRequestClass"); var param = {id : 1 }; var url = "demo.action";; ajax.post(param,url,function(response, options){});或者 : ajax.post(param,url,ajax.commonPrompt,function(response, options){});ajax.commonPrompt 會自動驗證返回的json的success并給出提示?
轉(zhuǎn)載于:https://www.cnblogs.com/hythzx/p/4654415.html
總結(jié)
以上是生活随笔為你收集整理的ExtJs 4.x Ajax简单封装的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java的LockSupport.par
- 下一篇: CodeForces 546B