在一个程序中需要Ext.Ajax.request同步请求,网上大多是使用ext-basex.js,但是我使用确实无法正常同步调用。所以查找到了其它方法,在此贴出,希望遇到与我同样问题的人可以有所借鉴:
Ext.Ajax.request在2.x是异步请求的,这样话在当前请求没有完成之前就不能在它的外部使用它的返回值,在下面这个表单验证中如果不是同步请求的话就有问题了,如:
var text = new Ext.form.TextField({@H_404_13@ fieldLabel : '名称',@H_404_13@ height : 23,@H_404_13@ width : 400,@H_404_13@ id : 'flowName',@H_404_13@ name : 'flowName',@H_404_13@ allowBlank : false,@H_404_13@ blankText : '名称不能为空',@H_404_13@ validateOnBlur : true,@H_404_13@ validationEvent : true,@H_404_13@ validator : function() {@H_404_13@ var resultValue;@H_404_13@ Ext.Ajax.request({@H_404_13@ url : '',@H_404_13@ sync:true,@H_404_13@ params : {@H_404_13@ name : text.getValue()@H_404_13@ },@H_404_13@ success : function(response,options) {@H_404_13@ var responseArray = Ext.util.JSON.decode(response.responseText);@H_404_13@ resultValue=responseArray.resultValue;@H_404_13@ }@H_404_13@ });@H_404_13@ if (resultValue!=null && resultValue!="") {@H_404_13@ text.invalidText = "该名称己经存在,请重新输入!";@H_404_13@ return false;@H_404_13@ } else {@H_404_13@ return true;@H_404_13@ }@H_404_13@ },@H_404_13@ anchor : '95%'@H_404_13@ });
上面是通过修改ext-base.js中的Ext.lib.Ajax.request来实现同步请求:
/**@H_404_13@ Adding a synchronous request to the Ext asynchronous on@H_404_73@ly mode of operation.
History: coded from Ext 2.2.
Additional configs.
@param {Object} options@H_404_13@ @config {Mixed} [sync] include this for a synchronous request@H_404_13@ */@H_404_13@ Ext.lib.Ajax.request = function(method,uri,cb,da@H_404_73@ta,options) {@H_404_13@ if(options){@H_404_13@ var hs = options.headers;@H_404_13@ if(hs){@H_404_13@ for(var h in hs){@H_404_13@ if(hs.hasOwnProperty(h)){@H_404_13@ this.initHeader(h,hs[h],false);@H_404_13@ }@H_404_13@ }@H_404_13@ }@H_404_13@ if(options.xmlData){@H_404_13@ if (!hs || !hs['Content-Type']){@H_404_13@ this.initHeader('Content-Type','text/xml',false);@H_404_13@ }@H_404_13@ method = (method ? method : (options.method ? options.method : 'POST'));@H_404_13@ da@H_404_73@ta = options.xmlData;@H_404_13@ }else if(options.jsonData){@H_404_13@ if (!hs || !hs['Content-Type']){@H_404_13@ this.initHeader('Content-Type','application/json',false);@H_404_13@ }@H_404_13@ method = (method ? method : (options.method ? options.method : 'POST'));@H_404_13@ da@H_404_73@ta = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;@H_404_13@ }@H_404_13@ }
return this["sync" in options ? "syncRequest" : "asyncRequest"](method,da@H_404_73@ta););//这句制定调用的方法,如果sync传递了就调用syncRequest, 否则调用原来的方法asyncRequest};
};
把下面这个方法加上,直接加在asyncRequest方法后面就可以,形式和asyncRequest相同,调用时如果需要同步调用加上sync:true,属性即可
/**@H_404_13@ Synchronous request. @H_404_13@ @H_404_13@ @param {Object} method@H_404_13@ @param {Object} uri@H_404_13@ @param {Object} callback@H_404_13@ @param {Object} postData@H_404_13@ */@H_404_13@ Ext.lib.Ajax.syncRequest = function(method,callback,postData)@H_404_13@ {@H_404_13@ var o = this.getConnectionObject();
if (!o) {@H_404_13@ return null;@H_404_13@ }@H_404_13@ else {@H_404_13@ o.conn.open(method,false);
if (this.useDefaultXhrHeader) {@H_404_13@ if (!this.defaultHeaders['X-Requested-With']) {@H_404_13@ this.initHeader('X-Requested-With',this.defaultXhrHeader,true);@H_404_13@ }@H_404_13@ }
if(postData && this.useDefaultHeader && (!this.hasHeaders || !this.headers['Content-Type'])){@H_404_13@ this.initHeader('Content-Type',this.defaultPostHeader);@H_404_13@ }
if (this.hasDefaultHeaders || this.hasHeaders) {@H_404_13@ this.setHeader(o);@H_404_13@ }
o.conn.send(postData || null);@H_404_13@ this.handleTransactionResponse(o,callback);@H_404_13@ return o;@H_404_13@ }@H_404_13@ };
但是在ext3.0的源码中已经找不到Ext.lib.Ajax.syncRequest方法。
所以在ext3.0中可以这样实现同步做:
var obj;
var value; if (window.ActiveXObject) { obj = new ActiveXObject('Microsoft.XMLHTTP'); } else if (window.XMLHttpRequest) { obj = new XMLHttpRequest(); } var url = '../../servlet/TestServlet‘;
obj.open('GET',url,false); obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); obj.send(null); value = obj.responseText;