PDA

View Full Version : Store Json Content Type



Brett89
3 Apr 2012, 7:50 PM
I've been having a terrible morning trying to get stores to work, and it just seems to be one problem after the next. </rant>

My current issue is that the data I am POSTing to the Web Service is not being sent as JSON, which results in a .NET error "Invalid JSON primitive". I've attached a screenie of firebug that shows one Web Service being hit with jquery (top one, works fine), and another Web Service being hit by Ext (currently only using Ext Charts, but will be migrating to purely Ext in the future).

I've included two views of both ajax requests - one for the Post tab, and another for the Headers tab. As you can see, the first request clearly has JSON being sent and the second does not, even though the headers seem to be exactly the same.



Ext.create('Ext.data.Store', {
storeId: 'OpenActivitiesByActivityType',
model: 'OpenActivitiesByActivityType',
proxy: {
type: 'ajax',
pageParam: undefined,
startParam: undefined,
limitParam: undefined,
extraParams: { "Key": "ID", "Value": "Label" },
url: '/Core/WebServices/Charts.asmx/OpenActivitiesByActivityType',
actionMethods: {
read: 'POST'
},
headers: {
'Content-Type': 'application/json;charset=utf-8',
accept: 'application/json, text/javascript, */*; q=0.01'
},
reader: {
type: 'json',
root: 'data'
}
},
autoLoad: true
});


That's my main issue, but I'd also like to know if there is a better approach to my workarounds for getting this to work with .NET. First I had to set the actionMethods.read to 'POST' because .NET Web Services don't like you going to them through GET. Then I had to set pageParam, startParam, and limitParam to undefined so that no parameters were sent to the Web Service. Web Services that don't have parameters work fine.

If this is indeed the best way to do it, is there a way that I can set these as defaults for proxy so I don't have to repeat it for every one of my stores?

Thanks for your time,
Brett.

skirtle
3 Apr 2012, 9:21 PM
I haven't tested this but if I've understood your requirements correctly then something along these lines might work:


Ext.define('CustomProxy', {
alias: 'proxy.custom',
extend: 'Ext.data.proxy.Ajax',

pageParam: undefined,
startParam: undefined,
limitParam: undefined,

buildRequest: function() {
var request = this.callParent(arguments);

// these are the options passed to Ext.Ajax.request, switch params for jsonData
request.jsonData = request.params;
request.params = {};

return request;
}
});

You should then be able to do something like this:


proxy: {
type: 'custom',
extraParams: { "Key": "ID", "Value": "Label" },
url: '/Core/WebServices/Charts.asmx/OpenActivitiesByActivityType',
...
},

Brett89
3 Apr 2012, 9:55 PM
Works flawlessly! Thanks!