PDA

View Full Version : JSON encoding form parameters



primary0
24 Feb 2011, 9:55 PM
I have a small issue with JSON encoding form parameters. When I submit my form, this is how I see the parameters in the web server's logs:


"{\"name\":\"David\",\"age\":35},"

As you might have guessed what I expect is something like this:


"{"name":"David","age":35},"

The data comes from a store and here's how I am doing it:


// Get JSON string from Store

var json = '';
store.each(function(store){
json += Ext.util.JSON.encode(store.data) + ',';
console.log(store.fields);
});
json = json.substring(0, json.length - 1);
console.log(json); // SHOWS PROPERLY FORMATTED JSON

// Assign parameters

var myParams = {
myField: json
}

// Submit the form

form.submit({
params: myParams,
success: function(form, action){
Ext.Msg.alert('Sucessful', action.result.message);
}
});

Any tips will be appreciated.

drian
24 Feb 2011, 10:20 PM
this will put the row's data in your json:


var data = [];
store.each(function(record) {
data.push(record.data);
//data.push({name_of_your_param: record.get('name_of_your_datastore_column'), age: record.get('age')}); // this is how you put only some part of your row's data in your json

});
data = Ext.encode(data);

primary0
24 Feb 2011, 10:41 PM
Hi drian.

Thanks a lot for your response. But my problem is not with getting the data from the store and creating a JSON string from it. That part works very well with your method and the one I am using too. However, something goes wrong here in my solution:


var myParams = {
myField: data
}

form.submit({
params: myParams,
...

Whatever is passed into the params property of the form is getting encoded again, or something like that, which is making the quotes get escaped in the string.

drian
24 Feb 2011, 11:03 PM
i think the .submit will encode your params again. I normally use an ajax request with



params: {
param1: Ext.encode(myForm.getForm().getValues()),
param2: Ext.encode(myArrayWithAdditionalData)
},


All in all, i pass all the params i need when i make the ajax call(form submit in your situation).

Condor
25 Feb 2011, 12:28 AM
A form submit uses application/x-www-form-urlencoded or multipart/form-data parameter format.

If you want application/json format then you will have to do an Ext.Ajax.request instead, e.g.

Ext.Ajax.request({
url: '...',
jsonData: form.getForm().getFieldValues(),
success: function(){
...
}
});

primary0
28 Feb 2011, 6:57 PM
Ext.Ajax.request was the same for me.


Ext.Ajax.request({
url: '/people',
jsonData: form.getForm().getFieldValues(),
success: function(){
...a


{"person"=>"[{\"name\":\"Some Guy\",\"age\":100}

I'm still trying to solve the problem. So far no luck at all.

primary0
28 Feb 2011, 8:03 PM
This is the solution I have at the moment:


var data = [];
data.push(form.getFieldValues());
Ext.Ajax.request({
jsonData: Ext.util.JSON.encode(data),
...

I found it from this website:
http://joekuan.wordpress.com/2010/12/06/posting-json-data-from-ext-js-to-php/

Condor
28 Feb 2011, 11:38 PM
No need to call encode(), just use:

var data = ...
Ext.Ajax.request({
jsonData: data,
...

primary0
28 Feb 2011, 11:46 PM
Perfect. Thanks guys.