-
31 Oct 2012 11:34 PM #1
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:
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: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" } });
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.Code:page=1&start=0&limit=25
Is anyone able to shed any light on whether this is possible? Thanks in advance
-
Best Answer Posted by skirtle
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'; } });
-
2 Nov 2012 11:01 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,121
- Vote Rating
- 453
- Answers
- 3160
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
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
2 Nov 2012 11:31 PM #3
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'; } });
-
6 Nov 2012 10:10 PM #4
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.


Reply With Quote