ITeye博客

Ext Ajax 同步的方法

techiewang · 2009-09-23 17:01 · extjs · Markdown

Ext.Ajax.request参数中并没有同步这个参数,但是很多时候需要同步请求来提供Componnet中的数据,而且没有add方法,比如 checkboxgroup 中动态的check选项,所以应该修改Ext.Ajax.request真正的请求方法Ext.lib.Ajax.request 具体代码来自EXT官方论坛: 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, false); } } } if(options.xmlData){ if (!hs || !hs){ 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){ 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(method, uri, cb, data);//这句制定调用的方法,如果sync传递了就调用syncRequest, 否则调用原来的方法asyncRequest};

syncRequest方法如下: 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) {
            this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
        }
    }

    if(postData && this.useDefaultHeader && (!this.hasHeaders || !this.headers)){
        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;
}

};

调用: Ext.Ajax.request({ url:'', scope:this, sync:true, success:function(){} });

评论(2)

  1. #2 楼 · piaoyaohou · 2009-10-27 19:49

    我用这个方法,测试始终failure

    郁闷!我是使用的Ext3.0

    
    
        function syncRequest(method, uri, callback, postData) {
                 var o = getConnectionObject() || null;
    
                if (o) {
                    o.conn.open(method, uri, true);
    
                    if (pub.useDefaultXhrHeader) {                    
                    	initHeader('X-Requested-With', pub.defaultXhrHeader);
                    }
    
                    if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers[contenttype])){
                        initHeader(CONTENTTYPE, pub.defaultPostHeader);
                    }
    
                    if (pub.defaultHeaders || pub.headers) {
                        setHeader(o);
                    }
    
                    //handleReadyState(o, callback);
                    o.conn.send(postData || null);
                    handleTransactionResponse(o, callback);
                }
                return o;
            }
            
    
    
  2. #1 楼 · techiewang · 2009-09-23 23:09

    ext ajax 同步官方网址: http://www.extjs.com/forum/showthread.php?p=195659

评论功能维护中,当前仅展示历史评论。