1. #1
    Ext JS Premium Member
    Join Date
    Mar 2011
    Posts
    2
    Vote Rating
    0
    cameron.murphy is on a distinguished road

      0  

    Default Answered: Cannot encode params as JSON in POST read request

    Answered: Cannot encode params as JSON in POST read request


    Hi, we're beginning a migration from 3.4 to 4.1 and I have hit a bit of a hitch when trying to request data for my grid panels. I have specified the store for the grid as follows:

    Code:
    var store = Ext.create("Ext.data.Store", {
        model: "Test",
        proxy: {
            type: "ajax",
            url: "/Test.svc",
            //headers: { 'Content-Type': 'application/json;charset=utf-8' },
            actionMethods: { read: "POST" },
            reader: "json",
            writer: "json"
        }
    });
    I'm not passing any of my own parameters for this test. I would expect (/love) to see the default parameters passed something like this:

    Code:
    { "page":1, "start":0, "limit":25 }
    However, they aren't. They are always like this:

    Code:
    page=1&start=0&limit=25
    Modifying the headers means that my server side code stops complaining about the format being incorrect (it expects it in JSON format) however it obviously fails when it tries to parse the parameters. I am assuming it is the writer that needs to be configured in some way to do this, but I cannot figure out how. Setting the writer's encode property to true does nothing.

    Is anyone able to shed any light on whether this is possible? Thanks in advance

  2. The writer isn't relevant in this case. Writers are used to serialize records for create and update requests.

    You may find the following useful:

    http://skirtlesden.com/articles/custom-proxies

    It includes an example of sending the parameters as JSON:

    Code:
    Ext.define('MyApp.proxy.MyAppProxy', {
        alias: 'proxy.myapp',
        extend: 'Ext.data.proxy.Ajax',
     
        buildRequest: function(operation) {
            var request = this.callParent(arguments);
    
            // For documentation on jsonData see Ext.Ajax.request
            request.jsonData = request.params;
            request.params = {};
     
            return request;
        },
    
        getMethod: function(request) {
            return 'POST';
        }
    });

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,647
    Vote Rating
    899
    Answers
    3575
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    This works (params in the POST params):

    Code:
    new Ext.data.Store({
        autoLoad : true,
        fields   : ['foo'],
        proxy    : {
            type          : 'ajax',
            url           : 'data/json.json',
            actionMethods : {
                read : 'POST'
            }
        }
    });
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,624
    Vote Rating
    331
    Answers
    550
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    The writer isn't relevant in this case. Writers are used to serialize records for create and update requests.

    You may find the following useful:

    http://skirtlesden.com/articles/custom-proxies

    It includes an example of sending the parameters as JSON:

    Code:
    Ext.define('MyApp.proxy.MyAppProxy', {
        alias: 'proxy.myapp',
        extend: 'Ext.data.proxy.Ajax',
     
        buildRequest: function(operation) {
            var request = this.callParent(arguments);
    
            // For documentation on jsonData see Ext.Ajax.request
            request.jsonData = request.params;
            request.params = {};
     
            return request;
        },
    
        getMethod: function(request) {
            return 'POST';
        }
    });

  5. #4
    Ext JS Premium Member
    Join Date
    Mar 2011
    Posts
    2
    Vote Rating
    0
    cameron.murphy is on a distinguished road

      0  

    Default


    Thank you both for your replies.

    @Skirtle: That was exactly what I was looking for. I have implemented it and it worked perfectly. The problem was without implementing my own proxy I was unable to encode the parameters passed as JSON.

Thread Participants: 2

Tags for this Thread