-
- /**
- *异步调用ajax,成功后返回值,作为回调函数的参数调用失败会提示
- *
- *@param{}
- *urlStr
- *paramsObj
- *callbackFunc
- */
- functionajaxCall(urlStr,paramsObj,callbackFunc){
- Ext.Ajax.request({
- url:urlStr,
- params:paramsObj,250); line-height:18px"> method:'POST',250); line-height:18px"> success:function(response){
- if(callbackFunc){
- varresult=Ext.util.JSON
- .decode(response.responseText);
- varcbfn=callbackFunc.createCallback(result);
- cbfn();
- }
- },250); line-height:18px"> failure:function(){
- Ext.Msg.alert("提示","方法调用失败");
- });
- }
*通用JS同步ajax调用返回jsonObject
*paramsStr为字符串键值对形式“key=value&key2=value2”
*@return{}返回jsonObject
functionajaxSyncCall(urlStr,paramsStr){
varobj;
varvalue;
if(window.ActiveXObject){
obj=newActiveXObject('Microsoft.XMLHTTP');
}elseif(window.XMLHttpRequest){
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',250); line-height:18px"> 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],85); font-weight:bold">if(options.xmlData){
if(!hs||!hs['Content-Type']){
this.initHeader('Content-Type','text/xml',250); line-height:18px"> method=(method?method:(options.method?options.method:'POST'));
data=options.xmlData;
if(options.jsonData){
'application/json',250); line-height:18px"> 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= {
varo=this.getConnectionObject();
if(!o){
returnnull;
else{
o.conn.open(method,85); font-weight:bold">if(this.useDefaultXhrHeader){
if(!this.defaultHeaders['X-Requested-With']){
this.initHeader('X-Requested-With',85); font-weight:bold">this.defaultXhrHeader,85); font-weight:bold">true);
if(postData&&this.useDefaultHeader&&(!this.hasHeaders||!this.headers['Content-Type'])){
this.defaultPostHeader);
this.hasDefaultHeaders||this.hasHeaders){
this.setHeader(o);
o.conn.send(postData||this.handleTransactionResponse(o,callback);
returno;
};
//调用
url:'',250); line-height:18px"> scope:this,250); line-height:18px"> sync:true,85); font-weight:bold">function(){}
});