PDA

View Full Version : Cannot encode params as JSON in POST read request



cameron.murphy
31 Oct 2012, 11:34 PM
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:



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:


{ "page":1, "start":0, "limit":25 }

However, they aren't. They are always like this:


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

mitchellsimoens
2 Nov 2012, 11:01 AM
This works (params in the POST params):


new Ext.data.Store({
autoLoad : true,
fields : ['foo'],
proxy : {
type : 'ajax',
url : 'data/json.json',
actionMethods : {
read : 'POST'
}
}
});

skirtle
2 Nov 2012, 11:31 PM
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:


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';
}
});

cameron.murphy
6 Nov 2012, 10:10 PM
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.