/** * 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) { //用于执行ajax请求,其中,func为第一个回调方法,一般用于执行成功提示,可以使用该类的commonPrompt,也可以直接处理成功请求后的业务,anotherFunc用于处理请求成功后执行的业务,可选 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('错误', '系统错误,请稍候再试!'); } }); },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('提示', "系统错误..."); } },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并给出提示