|
Ext.extend(Ext.data.EnhancedConnection,Ext.data.Connection,{
webServiceHeaders : {'Content-Type': 'application/json;utf-8'},
request : function(o){
if(this.fireEvent("beforerequest", this, o) !== false){
var p = o.params;
var hs = o.headers;
var url = o.url || this.url;
if(typeof url == 'function'){
url = url.call(o.scope||window, o);
}
var method = o.method||this.method||(p ? "POST" : "GET");
//是否为WebService
if(!o.isWebService){
//如果不是WebService请求,不改变EXT的请求行为
if(typeof p == "function"){
p = p.call(o.scope||window, o);
}
if(typeof p == "object"){
p = Ext.urlEncode(p);
}
if(this.extraParams){
var extras = Ext.urlEncode(this.extraParams);
p = p ? (p + '&' + extras) : extras;
}
if(o.form){
var form = Ext.getDom(o.form);
url = url || form.action;
var enctype = form.getAttribute("enctype");
if(o.isUpload || (enctype && enctype.toLowerCase() == 'multipart/form-data')){
return this.doFormUpload(o, p, url);
}
var f = Ext.lib.Ajax.serializeForm(form);
p = p ? (p + '&' + f) : f;
}
if(this.defaultHeaders){
hs = Ext.apply(hs || {}, this.defaultHeaders);
if(!o.headers){
o.headers = hs;
}
}
if(method == 'GET' && (this.disableCaching &&
o.disableCaching !== false) || o.disableCaching === true){
url += (url.indexOf('?') != -1 ? '&' : '?') + '_dc=' + (new Date().getTime());
}
Ext.lib.Ajax.setDefaultPostHeader(true);
}
else {
//如果有指定WebService方法名称
if(o.methodName){
url = url + "/" + o.methodName;
}
if(typeof p == "object"){
//使用不同的请求方式,使用不同的传参方式。
if(method == 'GET') {
p = Ext.urlEncode(p);
}
else{
p = Ext.util.JSON.encode(p);
}
}
hs = Ext.apply(hs || {},this.webServiceHeaders);
if(!o.headers){
o.headers = hs;
}
Ext.lib.Ajax.setDefaultPostHeader(false);
}
var cb = {
success: this.handleResponse,
failure: this.handleFailure,
scope: this,
argument: {options: o},
timeout : this.timeout
}
if((method == 'GET' && p) || o.xmlData || o.jsonData){
url += (url.indexOf('?') != -1 ? '&' : '?') + p;
p = '';
}
if(typeof o.autoAbort == 'boolean'){
if(o.autoAbort){
this.abort();
}
}
else if(this.autoAbort !== false){
this.abort();
}
this.transId = Ext.lib.Ajax.request(method, url, cb, p, o);
return this.transId;
}else{
Ext.callback(o.callback, o.scope, [o, null, null]);
return null;
}
}
});
|