- //异步ajax调用
- /**
- *异步调用ajax,成功后返回值,作为回调函数的参数调用失败会提示
- *
- *@param{}
- *urlStr
- *@param{}
- *paramsObj
- *@param{}
- *callbackFunc
- */
- functionajaxCall(urlStr,paramsObj,callbackFunc){
- Ext.Ajax.request({
- url:urlStr,
- params:paramsObj,
- method:'POST',
- success:function(response){
- if(callbackFunc){
- varresult=Ext.util.JSON
- .decode(response.responseText);
- varcbfn=callbackFunc.createCallback(result);
- cbfn();
- }
- },
- failure:function(){
- Ext.Msg.alert("提示","方法调用失败");
- }
- });
- }
- /**
- *通用JS同步ajax调用返回jsonObject
- *
- *@param{}
- *urlStr
- *@param{}
- *paramsStr为字符串键值对形式“key=value&key2=value2”
- *@return{}返回jsonObject
- */
- functionajaxSyncCall(urlStr,paramsStr){
- varobj;
- varvalue;
- if(window.ActiveXObject){
- obj=newActiveXObject('Microsoft.XMLHTTP');
- }elseif(window.XMLHttpRequest){
- obj=newXMLHttpRequest();
- }
- obj.open('POST',urlStr,false);
- obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
- obj.send(paramsStr);
- varresult=Ext.util.JSON.decode(obj.responseText);
- returnresult;
- }
- //EXTJS同步ajax调用
- varconn=Ext.lib.Ajax.getConnectionObject().conn;
- conn.open("GET",'yoururl',false);
- conn.send(null);
- alert(conn.responseText);
EXTJS 修改Ext.Ajax.request真正的请求方法Ext.lib.Ajax.request ,通过sync:true实现同步调用ajax
- Ext.lib.Ajax.request=function(method,uri,cb,data,options){
- if(options){
- varhs=options.headers;
- if(hs){
- for(varhinhs){
- if(hs.hasOwnProperty(h)){
- this.initHeader(h,hs[h],false);
- }
- }
- }
- if(options.xmlData){
- if(!hs||!hs['Content-Type']){
- this.initHeader('Content-Type','text/xml',false);
- }
- method=(method?method:(options.method?options.method:'POST'));
- data=options.xmlData;
- }elseif(options.jsonData){
- if(!hs||!hs['Content-Type']){
- this.initHeader('Content-Type','application/json',false);
- }
- method=(method?method:(options.method?options.method:'POST'));
- data=typeofoptions.jsonData=='object'?Ext.encode(options.jsonData):options.jsonData;
- }
- }
return this["sync" in options ? "syncRequest" : "asyncRequest"](method,data);//这句制定调用的方法,如果sync传递了就调用syncRequest, 否则调用原来的方法asyncRequest};
- Ext.lib.Ajax.syncRequest=function(method,callback,postData)
- {
- varo=this.getConnectionObject();
- if(!o){
- returnnull;
- }
- else{
- o.conn.open(method,false);
- if(this.useDefaultXhrHeader){
- if(!this.defaultHeaders['X-Requested-With']){
- this.initHeader('X-Requested-With',this.defaultXhrHeader,true);
- }
- }
- if(postData&&this.useDefaultHeader&&(!this.hasHeaders||!this.headers['Content-Type'])){
- this.initHeader('Content-Type',this.defaultPostHeader);
- }
- if(this.hasDefaultHeaders||this.hasHeaders){
- this.setHeader(o);
- }
- o.conn.send(postData||null);
- this.handleTransactionResponse(o,callback);
- returno;
- }
- };