PDA

View Full Version : POST json store with Ext.Ajax.request



art.com
18 Jul 2013, 2:24 AM
I've build a function in PHP that converts an array to CSV and promps the user to download it. I'd like to use an Ajax request to use this function to convert the store to a CSV file. In order to do this the Ajax request sends all the records of the store as a JSON string. I'm able to do this.

But here's my question. Currently I'm using Ext.encode twice because of the multidimensional array, and in my PHP I need to also decode twice.

Isn't there a better way to do this?


var date = Ext.Date.format(this.getDatepicker().getValue(), 'Y-m-d'),
store = this.getPlanEmpPlanDailyStore().load(),
dataRaw = store.getRange(),
me = this;

// Put raw data into array
var data = new Array();
for (var i=0,len=dataRaw.length; i<len; i++)
{
data[i] = Ext.encode(dataRaw[i].data);
}

Ext.Ajax.request({
url: 'data/print/csv.php',
method: 'POST',
params: {
date: date,
data: Ext.encode(data)
},
callback: function(options, success, response) {
console.log('Calback');
},
success: function(response, opts){
console.log('Success.');
},
failure: function(response, opts) {
console.log('Failure.');
}
});

julio.batista
19 Jul 2013, 3:25 AM
Looking at your code I don't think that you need to encode the data twice. Because your data is an array of encoded data, so you can only encode the entire array or convert the encoded array to a string.



var data = [
{name: "Teste 1", age: 12},
{name: "Teste 2", age: 16},
{name: "Teste 3", age: 15},
],
encoded = new Array(data.length);

for (var i = 0; i < data.length; i++)
{
encoded[i] = Ext.encode(data[i]);
}



console.log(encoded + "");


console.log(Ext.encode(encoded));


console.log(Ext.encode(data));