日期:2014-05-16 浏览次数:20401 次
//异步ajax调用 /** * 异步调用ajax,成功后返回值,作为回调函数的参数 调用失败会提示 * * @param {} * urlStr * @param {} * paramsObj * @param {} * callbackFunc */ function ajaxCall(urlStr, paramsObj, callbackFunc) { Ext.Ajax.request({ url : urlStr, params : paramsObj, method : 'POST', success : function(response) { if (callbackFunc) { var result = Ext.util.JSON .decode(response.responseText); var cbfn = callbackFunc.createCallback(result); cbfn(); } }, failure : function() { Ext.Msg.alert("提示", "方法调用失败"); } }); }
/** *通用JS 同步ajax调用 返回json Object * * @param {} * urlStr * @param {} * paramsStr 为字符串键值对形式“key=value&key2=value2” * @return {} 返回json Object */ function ajaxSyncCall(urlStr, paramsStr) { var obj; var value; if (window.ActiveXObject) { obj = new ActiveXObject('Microsoft.XMLHTTP'); } else if (window.XMLHttpRequest) { obj = new XMLHttpRequest(); } obj.open('POST', urlStr, false); obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); obj.send(paramsStr); var result = Ext.util.JSON.decode(obj.responseText); return result; }
?
//EXTJS 同步ajax调用 var conn = Ext.lib.Ajax.getConnectionObject().conn; conn.open("GET", 'your url',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){
var hs = options.headers;
if(hs){
for(var h in hs){
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;
}else if(options.jsonData){
if (!hs || !hs['Content-Type']){
this.initHeader('Content-Type', 'application/json', false);
}
method = (method ? method : (options.method ? options.method : 'POST'));
data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
}
}
?
?return this["sync" in options ? "syncRequest" : "asyncRequest"](method, uri, cb, data);//这句制定调用的方法,如果sync传递了就调用syncRequest, 否则调用原来的方法asyncRequest};
Ext.lib.Ajax.syncRequest = function(method, uri, callback, postData) { var o = this.getConnectionObject(); if (!o) { return null; } else { o.conn.open(method, uri, 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); return o; } };
?
?