PDA

View Full Version : Ajax Store - Send Params as JSON Body



strickjb
17 Apr 2012, 6:05 AM
Is there a way to set up an Ajax Store so that it sends params as JSON in the body of an AJAX request.

I'm looking to mimic the functionality of...


Ext.Ajax.request({
url: '/foo',
jsonData: jsonObject
});

But as a Ajax Store, such as:


Ext.define('My.Store',
extend: 'Ext.data.Store',
model: 'bar',
proxy: {
type: 'ajax',
url: 'foo.json',
reader: 'json'
}
});

Except right now, the store will send the parameters as GET params.

These parameters are going to be pretty complex which is why I'd like to serialize them as JSON, preferably not under a single GET/POST param.

Any ideas?

scottmartin
17 Apr 2012, 6:21 AM
Try moving your proxy from the store to the model.

Regards,
Scott.

strickjb
17 Apr 2012, 6:26 AM
It didn't make a difference. Parameters are added to the querystring instead of serializing the object to the request body.

strickjb
17 Apr 2012, 10:26 AM
I guess I could just be super hackish and override methods...

strickjb
17 Apr 2012, 11:14 AM
Okay, here's my hack...


Ext.define('My.Store',
extend: 'Ext.data.Store',

...

proxy: {
type: 'ajax',

...

//Add these two properties
actionMethods: {
read: 'POST'
},

jsonData: true //Custom, used in override
}
});

I really didn't want to override such a large method but it was the best entry point.


Ext.override(Ext.data.proxy.Ajax, {

doRequest: function(operation, callback, scope) {
var writer = this.getWriter(),
request = this.buildRequest(operation, callback, scope);

if (operation.allowWrite()) {
request = writer.write(request);
}

Ext.apply(request, {
headers : this.headers,
timeout : this.timeout,
scope : this,
callback : this.createRequestCallback(request, operation, callback, scope),
method : this.getMethod(request),
disableCaching: false // explicitly set it to false, ServerProxy handles caching
});

//Added... jsonData is handled already
if(this.jsonData) {
request.jsonData = Ext.encode(request.params);
delete request.params;
}

Ext.Ajax.request(request);

return request;
}
});

Misiu
3 Jun 2013, 5:26 AM
I would like to get more info on this one.
I'm using ASP.NET Classic as my server side (I can't switch to MVC4, really!)

This hack works perfect for me, but maybe till now it is build in into ExtJS core classes?
Overrides works for some time, if something will change in core classes this might stop working.

@Scott any news on this? I know that ASP.NET Classic is an old technology, but adding one parameter to Ajax proxy can't be so hard, especially @strickjb posted nice solution.