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

Thread: Ajax, XML, POST and PUT, Content Type

  1. #1
    Sencha Premium User
    Join Date
    Apr 2007
    Location
    Vienna, Austria
    Posts
    59

    Default [FIXED] Ajax, XML, POST and PUT, Content Type

    Hi -

    I am trying to POST and PUT some XML data. Looks like this bug
    is also in the final version of Ext JS 2.0 and I am trying to find a work-around. If I am doing this:

    Code:
    Ext.lib.Ajax.setDefaultPostHeader(false);
    Ext.Ajax.request(
        {
            url: 'http://' + window.location.host + '/ews/v0/events',
            method: 'POST',
            xmlData: xmlModel,                                        
            success: handleSuccess,
            failure: handleFailure
        }
    );
    the Content-Type is set to text/xml instead of being empty. If I do this:

    Code:
    Ext.lib.Ajax.setDefaultPostHeader(false);
    Ext.Ajax.request(
        {
            url: 'http://' + window.location.host + '/ews/v0/events',
            method: 'POST',
            headers: {'Content-Type': 'application/xml'},
            xmlData: xmlModel,                                        
            success: handleSuccess,
            failure: handleFailure
        }
    );
    the the Content-Type is set to text/xml,application/xml. Which I think is illegal and in any case not what I want. If I leave out setDefaultPostHeader(false) and the headers config option the Content-Type becomes something like application/x-www-form-urlencoded,text/xml which is totally wrong for posted xml data.

    And as was mentioned in another thread recently setting the method to PUT does not work at all.

    Could these issues please be fixed? I find them very confusing and they also limit the usefulness of Ext JS as an Ajax client library in a serious way. At least for me

  2. #2
    Ext User santosh.rajan's Avatar
    Join Date
    Sep 2007
    Location
    Kannur, Kerala, India
    Posts
    611

    Default

    I have posted xml data on many occasions without specifying any headers (Ext uses the defaults) with no problems. Whats your problem? I dont understand.
    Make everything as simple as possible, but not simpler.
    - Albert Einstein

  3. #3
    Sencha Premium User
    Join Date
    Apr 2007
    Location
    Vienna, Austria
    Posts
    59

    Default

    Quote Originally Posted by santosh.rajan View Post
    Whats your problem? I dont understand.
    I would like to PUT and POST xml to a web service using Ext's Ajax object and have a header like this one

    Code:
    Content-type: application/xml; charset="ebcdic-fr-297+euro"
    included

  4. #4
    Ext User santosh.rajan's Avatar
    Join Date
    Sep 2007
    Location
    Kannur, Kerala, India
    Posts
    611

    Default

    Are you saying that the web service does not handle your post because of this?
    Make everything as simple as possible, but not simpler.
    - Albert Einstein

  5. #5
    Sencha Premium User
    Join Date
    Apr 2007
    Location
    Vienna, Austria
    Posts
    59

    Default

    Quote Originally Posted by santosh.rajan View Post
    Are you saying that the web service does not handle your post because of this?
    Now this sounds like you trying to set the standard for acceptable quality A bug is a bug is a bug and not being able to PUT is a missing feature.

    But seriously: my web service throws an illegal content type exception. Which I could fix somehow, but then...

  6. #6
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854

    Default

    There is at least one other thread discussing an override to allow you to do a PUT.

  7. #7
    Sencha Premium User
    Join Date
    Apr 2007
    Location
    Vienna, Austria
    Posts
    59

    Default

    Quote Originally Posted by tryanDLS View Post
    There is at least one other thread discussing an override to allow you to do a PUT.
    Yes, thanks, I know. But what about the Content-Type?

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

    Lightbulb About the Content-Type

    Hi

    You are right, tellioglu. From code source of function request in Ext.lib.Ajax, there are two 'magic' issues if xmlData os jsonData is specified:
    1. method POST is hardcoded
    2. Content-Type is added


    as we can see in:
    Code:
            if(options.xmlData){
                this.initHeader('Content-Type', 'text/xml', false);
                method = 'POST';
                data = options.xmlData;
            }else if(options.jsonData){
                this.initHeader('Content-Type', 'text/javascript', false);
                method = 'POST';
                data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
            }
    You can try this override:
    Code:
    Ext.override(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){
                        if (!hs['Content-Type']){
                            this.initHeader('Content-Type', 'text/xml', false);
                        }
                    }
                    if (!method) method='POST';
                    data = options.xmlData;
                }else if(options.jsonData){
                    if (hs){
                        if (!hs['Content-Type']){
                            this.initHeader('Content-Type', 'text/javascript', false);
                        }
                    }
                    if (!method) method='POST';
                    data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
                }
            }
            return this.asyncRequest(method, uri, cb, data);
        }
    });

    I have not try this code, so you can give me your feedback about this.

    In Ext.lib.Ajax there is another 'hardcoding' of Content-Type in combination with xmlData and/or jsonData. To bypass it you must set
    Code:
    Ext.lib.Ajax.setDefaultPostHeader(false);
    or add to your request-ajax config object the property
    Code:
    ...
    useDefaultHeader:false,
    ...
    Hope this can help you

  9. #9
    Sencha User
    Join Date
    Apr 2012
    Location
    Austin, Texas
    Posts
    4

    Default

    The method and content-type hard-coding are both fixed in SVN for all adapters.

  10. #10
    Sencha User
    Join Date
    Jan 2008
    Posts
    46

    Default

    Does this mean that non svn-subscribers won't have the update?

    I am having the same problem with Ajax, in my case I need to set Content-Type to 'application/json' as .Net WCF service understands only this header.

    Do I have to manually change the source of Ext?

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
  •