JavaScript API and character encoding


(Jamie Smith) #1

When I do a JS API call to set data, not all characters are encoding correctly. For example, using setMetadata() to set the value of a metadata text field to £10 results in £10. It works as expected when I go via the admin interface. Has anyone else experienced this and have any suggestions for possible fixes?

 

Many thanks.

 

Matrix version: 4.14.1


(Nic Hubbard) #2

Can you use Firebug or Charles to see what is getting sent in the POST request? Just curious to see if it is already screwed up when it gets sent to the server, or if it is after.


(Tom Chadwin) #3

Is your source encoded correctly?


(Jamie Smith) #4

Can you use Firebug or Charles to see what is getting sent in the POST request? Just curious to see if it is already screwed up when it gets sent to the server, or if it is after.

 

I inspected the POST data and the £ character is being encoded as %C2%A3 in the query string. The response is:

{"success":["Metadata field #123456 has been successfully set to \"\u00c2\u00a310\" for Asset \"Foo\" (#654321)"]}

We're not set to UTF-8. Could that have anything to do with it?


(Nic Hubbard) #5

 

I inspected the POST data and the £ character is being encoded as %C2%A3 in the query string. The response is:

{"success":["Metadata field #123456 has been successfully set to \"\u00c2\u00a310\" for Asset \"Foo\" (#654321)"]}

We're not set to UTF-8. Could that have anything to do with it?

 

All requests that the JS API makes use the javascript encodeURI() function on the  sent data and URL. This is what is changing the £ character to %C2%A3.

 

You can replicate it by doing:

var pound = '£';
alert(encodeURI(pound));

Example: http://jsfiddle.net/zedsaid/bc7vhxb1/

 

I guess the only solution, and actually good practice, is to use £ instead.


(Jamie Smith) #6

I guess the only solution, and actually good practice, is to use £ instead.

 

Thanks, Nic.


(Jamie Smith) #7

I guess the only solution, and actually good practice, is to use £ instead.

 

I've been looking into this problem further and am still unable to make it work. It seems that converting the £ symbol to £ prior to making the JS API call causes it to be saved as £ (as evidenced by checking the source), and it's being interpreted literally when output by Matrix. The only way around this problem that I can think of is editing the JS API file to make all data submissions work similar to how WYSIWYG content is sent (the issue doesn't occur when editing WYSIWYG data).

 

Any thoughts on alternative strategies welcome. Many thanks.


(Nic Hubbard) #8

 

I've been looking into this problem further and am still unable to make it work. It seems that converting the £ symbol to £ prior to making the JS API call causes it to be saved as £ (as evidenced by checking the source), and it's being interpreted literally when output by Matrix. The only way around this problem that I can think of is editing the JS API file to make all data submissions work similar to how WYSIWYG content is sent (the issue doesn't occur when editing WYSIWYG data).

 

Any thoughts on alternative strategies welcome. Many thanks.

 

That is frustrating.