ExtJs ajax调用汇总(转)
1 //异步ajax调用 2 /** 3 * 异步调用ajax,成功后返回值,作为回调函数的参数 调用失败会提示 4 * 5 * @param {} 6 * urlStr 7 * @param {} 8 * paramsObj 9 * @param {} 10 * callbackFunc 11 */ 12 function ajaxCall(urlStr, paramsObj, callbackFunc) { 13 Ext.Ajax.request({ 14 url : urlStr, 15 params : paramsObj, 16 method : 'POST', 17 success : function(response) { 18 if (callbackFunc) { 19 var result = Ext.util.JSON 20 .decode(response.responseText); 21 var cbfn = callbackFunc.createCallback(result); 22 cbfn(); 23 } 24 }, 25 failure : function() { 26 Ext.Msg.alert("提示", "方法调用失败"); 27 } 28 }); 29 }
1 /** 2 *通用JS 同步ajax调用 返回json Object 3 * 4 * @param {} 5 * urlStr 6 * @param {} 7 * paramsStr 为字符串键值对形式“key=value&key2=value2” 8 * @return {} 返回json Object 9 */ 10 function ajaxSyncCall(urlStr, paramsStr) { 11 var obj; 12 var value; 13 if (window.ActiveXObject) { 14 obj = new ActiveXObject('Microsoft.XMLHTTP'); 15 } else if (window.XMLHttpRequest) { 16 obj = new XMLHttpRequest(); 17 } 18 obj.open('POST', urlStr, false); 19 obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 20 obj.send(paramsStr); 21 var result = Ext.util.JSON.decode(obj.responseText); 22 return result; 23 }
1 //EXTJS 同步ajax调用 2 var conn = Ext.lib.Ajax.getConnectionObject().conn; 3 conn.open("GET", 'your url',false); 4 conn.send(null); 5 alert(conn.responseText);
EXTJS 修改Ext.Ajax.request真正的请求方法Ext.lib.Ajax.request ,通过sync:true实现同步调用ajax
1 Ext.lib.Ajax.request = function(method, uri, cb, data, options) { 2 if(options){ 3 var hs = options.headers; 4 if(hs){ 5 for(var h in hs){ 6 if(hs.hasOwnProperty(h)){ 7 this.initHeader(h, hs[h], false); 8 } 9 } 10 } 11 if(options.xmlData){ 12 if (!hs || !hs['Content-Type']){ 13 this.initHeader('Content-Type', 'text/xml', false); 14 } 15 method = (method ? method : (options.method ? options.method : 'POST')); 16 data = options.xmlData; 17 }else if(options.jsonData){ 18 if (!hs || !hs['Content-Type']){ 19 this.initHeader('Content-Type', 'application/json', false); 20 } 21 method = (method ? method : (options.method ? options.method : 'POST')); 22 data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData; 23 } 24 }
return this["sync" in options ? "syncRequest" : "asyncRequest"](method, uri, cb, data);//这句制定调用的方法,如果sync传递了就调用syncRequest, 否则调用原来的方法asyncRequest};
1 Ext.lib.Ajax.syncRequest = function(method, uri, callback, postData) 2 { 3 var o = this.getConnectionObject(); 4 5 if (!o) { 6 return null; 7 } 8 else { 9 o.conn.open(method, uri, false); 10 11 if (this.useDefaultXhrHeader) { 12 if (!this.defaultHeaders['X-Requested-With']) { 13 this.initHeader('X-Requested-With', this.defaultXhrHeader, true); 14 } 15 } 16 17 if(postData && this.useDefaultHeader && (!this.hasHeaders || !this.headers['Content-Type'])){ 18 this.initHeader('Content-Type', this.defaultPostHeader); 19 } 20 21 if (this.hasDefaultHeaders || this.hasHeaders) { 22 this.setHeader(o); 23 } 24 25 o.conn.send(postData || null); 26 this.handleTransactionResponse(o, callback); 27 return o; 28 } 29 };
1 //调用 2 Ext.Ajax.request({ 3 url:'', 4 scope:this, 5 sync:true, 6 success:function(){} 7 });