PDA

View Full Version : Problem in creating JSON dynamically



seema
20 Dec 2011, 11:28 AM
Hi, I have a scenario, where I have to pass dynamically created JSON to my server as post request.
I'm trying to generate JSON object using java script. my code is like that.


// Initialisation to generate JSON like '{"filters": [{"partner":[{"HomeGeo":["EMEA","USA"]}]}]}'

filterObj={"filters": []};
filterObj.pdb.user.search.filters.splice(0,0, {"partner":[]});

// populate filters
// filterOn="partner"
// filterIndex="HomeGeo"
// filterValue= ["EMEA","USA"]

function populateFilter(filterOn,filterIndex,filterValue){

filterObj.pdb.user.search.filters[0][filterOn][filterIndex]=filterValue;

console.log(filterObj.pdb.user.search.filters[0][filterOn][filterIndex]);
}

function callFilter(){
var myparams = { 'filterObj': filterObj };

Ext.Ajax.request({

url:'/api/filter.json',

params: Ext.encode(myparams),

method: 'POST',

headers: {
'Content-Type': 'application/json; charset=utf-8' },

success:
function(resp) {

console.log(resp.responseText);

}

});

}


problem is, my filterObj is not showing the filter Index and filter values. while in the console.log is showing the proper values.

if I use splice or push method, then the values are populating. but with these methods I should know my key. and in my scenario I have to pass the key from a variable.

can you suggest me proper way to achieve this.

mitchellsimoens
20 Dec 2011, 11:32 AM
Have you checked out jsonData config for Ext.Ajax.request?

seema
20 Dec 2011, 11:44 AM
json data config is correct as per my knowledge. I can see the filterObj as a part of JSON post. rest of the string is there. only "partner" values are missing.

I'm getting this JSON


{"filters":[{"partner":[]}]}