简体   繁体   中英

JavaScript — encode object with special characters

I have an ExtJS grid with an export functionality for which the selected row/record object is submitted. The issue is there is one column that sometimes has special chars, in particular the less than char < and because of this the encoded string is unexpectedly terminated.

Here is my code for submitting the download:

Ext.dom.Query.selectNode("iframe[name=myiframe]").src = myDownloadUrl +"?records=" + Ext.encode(records);

Incomplete encoded records object after submitting download when < char is present:

records:[{"id":1141,"view_name":"freemarker","view_value":"<

So the string gets terminated at the very first < char.

In rows where there is no < char the whole record object is submitted and everything works fine.

thanks

Try urlEncoding records with Ext.Object.toQueryString(records)

Example from documentation :

Ext.Object.toQueryString({foo: 1, bar: 2}); // returns "foo=1&bar=2"
Ext.Object.toQueryString({foo: null, bar: 2}); // returns "foo=&bar=2"
Ext.Object.toQueryString({'some price': '$300'}); // returns "some%20price=%24300"
Ext.Object.toQueryString({date: new Date(2011, 0, 1)}); // returns "date=%222011-01-01T00%3A00%3A00%22"
Ext.Object.toQueryString({colors: ['red', 'green', 'blue']}); // returns "colors=red&colors=green&colors=blue"

If you are encoding an array of objects you can:

Ext.Object.toQueryString({data:records},true)

and you will have the data encoded like data[0][id]=1141,data[0][view_name]=freemarker,data[1][id]=232,data[1][view_name]=abc,etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM