Kumar Bibek
Kumar Bibek

Reputation: 9127

Sencha Touch: Using proxy and POST request and URL generation

I have been trying to do a post request using a proxy. I have tried the direct proxy, rest and ajax proxy, and haven't been able to find a working example for a POST request.

  1. Is it possible? Because all the examples that I have seen seen to be using only GET.
  2. Any working examples, or pointers in this direction?

Also, I couldn't figure what is the correct way to generate URLs for a proxy at run-time, for example, calling a function to return the URL.

Upvotes: 1

Views: 3787

Answers (3)

arthurakay
arthurakay

Reputation: 5651

If you look at the source code for Ext.data.proxy.Rest you'll see a config object for actionMethods. They're not documented, but you should be able to pass that as a config on your proxy to override it.

For example:

        proxy: {
        type: 'ajax',
        url: 'path/to/foo',

        actionMethods: {
            create : 'POST',
            read   : 'POST',
            update : 'PUT',
            destroy: 'DELETE'
        },

        reader: {
            type: 'json',
            rootProperty: 'root',
            totalProperty : 'totalCount'
        }
    }

Upvotes: 1

ThinkFloyd
ThinkFloyd

Reputation: 5021

Easiest example of POST request could be like this:

var obj = new Object();
obj.userId = username;
obj.password = password;
var data = Ext.JSON.encode(obj);
Ext.Ajax.request({
    url : 'http://myservice/auth/login?_type=json', // url : this.getUrl(),
    method : "POST",
    headers: {
        'Content-Type': 'application/json'
    },
    params : data,
    useDefaultXhrHeader : false,
    withCredentials: true,
    success : function(response) {
        Ext.Msg.alert("Success", "Welcome "+respObj.user.name);
    },
    failure : function(response) {
        var respObj = Ext.JSON.decode(response.responseText);
        Ext.Msg.alert("Error", respObj.status.statusMessage);
    }
});

Please note here you can customize url as per your convenience.

Upvotes: 0

Related Questions