Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: [2.1][FIXED] Ext.Ajax.request jsonData option does not submit data

  1. #1

    Default [2.1][FIXED] Ext.Ajax.request jsonData option does not submit data

    In upgrading to from 2.0.2 to 2.1 the jsonData parameter of Ext.Ajax.request no longer functions properly for me.

    simplified example code:
    Code:
    Ext.onReady(function(){
        Ext.Ajax.request({
            url: '/test/save',
            jsonData: {"title": "Invoices"}
        });
            
    })
    When running this code against 2.0.2 ext-all-debug.js and ext-base.js adapter I can verify with firebug and a web server that a POST request was sumbitted with "{"title": "Invoices"}" in the body. The Content-Type headers are set to "application/x-www-form-urlencoded,text/javascript"

    When running against 2.1 a GET request is performed with Content-Type set to "application/json". (I understand that this behavior has changed intentionally between revisions, I offer the headers as proof that Ext is attempting to process the jsonData field.) There is no data in the document body or GET or POST paremeters (other than _dc).

    I like this library a lot and appreciate all the hard work you guys put in on it.
    Thank you.

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    There is no document body for a GET.

  3. #3
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    OK, you're aying it should select POST if jsonData is specified? Yes, that sounds right.

  4. #4
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    Try this as an override (My changes in red):

    Code:
    Ext.override(Ext.data.Connection, {
        request : function(o){
            if(this.fireEvent("beforerequest", this, o) !== false){
                var p = o.params;
    
                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;
                }
    
                var url = o.url || this.url;
                if(typeof url == 'function'){
                    url = url.call(o.scope||window, o);
                }
    
                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;
                }
    
                var hs = o.headers;
                if(this.defaultHeaders){
                    hs = Ext.apply(hs || {}, this.defaultHeaders);
                    if(!o.headers){
                        o.headers = hs;
                    }
                }
    
                var cb = {
                    success: this.handleResponse,
                    failure: this.handleFailure,
                    scope: this,
                    argument: {options: o},
                    timeout : o.timeout || this.timeout
                };
    
                var method = o.method||this.method||((p || o.xmlData || o.jsonData) ? "POST" : "GET");
    
                if(method == 'GET' && (this.disableCaching && o.disableCaching !== false) || o.disableCaching === true){
                    url += (url.indexOf('?') != -1 ? '&' : '?') + '_dc=' + (new Date().getTime());
                }
    
                if(typeof o.autoAbort == 'boolean'){ // options gets top priority
                    if(o.autoAbort){
                        this.abort();
                    }
                }else if(this.autoAbort !== false){
                    this.abort();
                }
                if((method == 'GET') && p){
                    url += (url.indexOf('?') != -1 ? '&' : '?') + p;
                    p = '';
                }
                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;
            }
        }
    });

  5. #5

    Default

    Your patch works for me.

    It has however created another bug (I think). If disableCaching is enabled (which it is by default) the '_dc' URL parameter is no longer included in the request. My assumption is that this will extend to all additional GET parameters (pardon me if I am using incorrect terminology here. specifically _dc=#...# shows up as a GET parameter in a pylons request object for me)

    Behavior in 2.0.2:
    json string available in POST body and _dc=somenumber as a GET parameter

    Behavior in 2.1 with patch:
    json string available in POST body and no additional GET parameters

    Thank you for the prompt reply

  6. #6
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    The _dc=??? is not necessary for POST requests. Post requests are not cached.

  7. #7

    Default

    Could this be put into svn or changing of this lines was about REST support?

  8. #8
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    That's up to the Ext developers. Hopefully Brian will comment on this thread.

  9. #9
    Ext User halkon_polako's Avatar
    Join Date
    Nov 2007
    Location
    Barcelona
    Posts
    193

    Default

    @Animal

    About your overrides, I'm agree with you about the first 'red' line: if a method is not specified, then it resolves the default method. In this case, if only jsonData or xmlData or params are specified, then POST must be the default. Good!.

    About the second 'red' line:
    Code:
     if((method == 'GET') && p){
    With this modification we are appending parameters to the URL query string only when method is GET (and parameters exist). So , in the case of another kind of method (PUT, HEAD..), these parameters are lost. Not good.
    We can think also in a request with params and jsonData/xmlData...as jsondata and xmldata must go in the document body, where are the parameters? Their place is in the query string.

    The correction of the bug is your first 'red' line: to detect the default method.

  10. #10
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    Ah, OK. I don't use other methods. Well, let's hope Brian can sort this out. Looking at the amount of ifs and buts in that method, it's not surprising it's taken this long to get it to cover all bases!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •