PDA

View Full Version : Ext.urlEncode does not encode objects



killfill
16 Apr 2011, 8:09 AM
Dont know if this is a bug or a feature request...

But i noticed that
Ext.urlEncode({k1: 1, sub: {subk1: 1, subk2: 2}}) will produce
"k1=1&sub=%5Bobject%20Object%5D".

I need to add some extra data before submitting the form to the server.
I think the most clean way in my case, is to send it as a hash. Maybe is a good idea to make urlEncode produce
"k1=1&sub%5Bsubk1%5D=1&sub%5Bsubk2%5D=2"

i.e. "k1=1&sub[subk1]=1&sub[subk2]=2.

Im using this, wich is working just for for me.


Thanks!



Ext.urlEncode = function(o, pre, keyPrefix){
var empty,
buf = [],
e = encodeURIComponent;

Ext.iterate(o, function(key, item){
empty = Ext.isEmpty(item);
Ext.each(empty ? key : item, function(val){
key = keyPrefix? keyPrefix + '['+key+']' : key;
if (Ext.isObject(item))
buf.push('&',Ext.urlEncode(item, pre, key))
else if (Ext.isArray(item))
item.forEach(function(i) {
buf.push('&', Ext.urlEncode(i, pre, key));
});
else
buf.push('&', e(key), '=', (!Ext.isEmpty(val) && (val != key || !empty)) ? (Ext.isDate(val) ? Ext.encode(val).replace(/"/g, '') : e(val)) : '');
});
});
if(!pre){
buf.shift();
pre = '';
}
return pre + buf.join('');
};

VinylFox
17 Apr 2011, 2:32 PM
That is quite interesting, what kind of server do you have that understands this format of query string?

What your doing is non-standard, so I thinks its unlikely support will be added. The IETF RFC on URI's clearly states that the 'query' portion of the URI cannot contain hierarchical data:

http://tools.ietf.org/html/rfc3986#section-3.4

killfill
17 Apr 2011, 2:36 PM
Oh im not sure if its a standart..

I remember rails did use it like this.

Im currently using express (node), wich is working fine so far :)

VinylFox
17 Apr 2011, 4:33 PM
Interesting, ill have to check that out.