PDA

View Full Version : passing json to store.load method



schmidan
8 Sep 2009, 8:34 AM
Hello ExtJS Community,

I have been using ExtJS 3.0 RC2 for some time and used to call JSON enabled Web Services in connection with ExtJS stores by calling store.load and passing a JSON encoded object as param, i.e.:



var fooStore = new Ext.data.Store({
reader: new Ext.data.JsonReader({
root: 'fooRoot',
idProperty: 'id',
fields: ['id', 'foo1', 'foo2']
}),
url: 'fooUrl',
autoLoad: false
});
fooStore.load({
params: Ext.encode({
fooParam: 1
})
});

This worked perfectly well until switching to ExtJS 3.0 Final (I think it also used to work in the 2.x versions). Now, the params are simply ignored and an HTTP GET is sent, making my service calls fail.

So, how can I get this working again? - Any help is much appreciated! Thanks a lot in advance!

Cheers,
Andy

Jack9
8 Sep 2009, 9:09 AM
It can probably all be solved by adding:

,method: 'POST'

The encoded args probably dont get passed with a GET (because they arent a simple string). You have to specify POST.


var fooStore = new Ext.data.Store({
reader: new Ext.data.JsonReader({
root: 'fooRoot',
idProperty: 'id',
fields: ['id', 'foo1', 'foo2']
}),
url: 'fooUrl',
method: 'POST',
autoLoad: false
});
fooStore.load({
params: Ext.encode({
fooParam: 1
})
});

schmidan
8 Sep 2009, 10:25 PM
Hi Jack,

The switch to POST is done as soon as params are passed to the load method. So, afaik nothing special must be done. - Anyway, when encoding the params in JSON and passing them to the load method, they are simply ignored and a GET request is issued (since it is assumed that no params are present).

The confusing thing is that my code snippet has worked until ExtJS 3.0 RC2 but does not work in ExtJS 3.0 Final anymore. So, what is the purpose of this change and how can I get the "old" behavior for the load method in connection with JSON encoded params?

Thanks a lot again! Cheers,
Andy

Jack9
9 Sep 2009, 12:12 PM
The code I posted works for me. I am using the stock Ext3.0 final release =/
Perhaps you have an include conflict?

schmidan
9 Sep 2009, 10:03 PM
Hi Jack,

Thanks for your reply!

- However, still the same issue. When pasting and running your code, Firebug tells me a GET has been issued and the JSON encoded params are simply not passed (they do not appear in the request at all).
By the way, when simply passing the params without encoding them to JSON before, the whole thing works, i.e. a POST is issued and the (URL encoded) params are sent.

Here come my includes:


<link rel="stylesheet" type="text/css" href="lib/ext/resources/css/ext-all.css" />
<script type="text/javascript" src="lib/ext/adapter/ext/ext-base.js">
</script>
<script type="text/javascript" src="lib/ext/ext-all-debug.js">
</script>

Finally, when having done the switch from RC2 to Final, I've simply replaced the libs, nothing else.

Thanks again! Cheers,
Andreas

schmidan
16 Sep 2009, 8:33 AM
Hello ExtJS Community,

Found the reason (simply debugged both versions with Firebug). There is a difference between the release candidate and the release:

When calling fooStore.load and passing the JSON encoded string, this ends up in Store's execute function. In line 30832 Ext.apply is called with 3 params:
1. an empty object the params are applied to,
2. options.params containing our JSON encoded string, and
3. this.baseParams

In short:

var params = Ext.apply({}, options.params, this.baseParams);

Argument 3 - this.baseParams - is the show stopper, since it causes Ext.apply in line 36 (ext-base-debug.js) to skip argument c which contains our JSON encoded string. From this moment on, our JSON encoded string is gone, finally causing the GET request to be issued, since no params are found in further processing.

The release candidate works differently. There, the fatal line looks like this (line 12102):

var params = Ext.apply(options.params || {}, this.baseParams);

That means passing whatever params to a load call does not have any effect, since these are skipped. So, does this behavior qualify to be added to the framework's issue list?

Cheers,
Andy

Jack9
16 Sep 2009, 3:19 PM
I would hope so! Some of my code will break if I used the RC.