We help IT Professionals succeed at work.

javascript pass Chinese characters fails

I generated a javascript code which is called from the brwoser.

When I check inside the IDE, everything looks good

var LeftBarcoalHtml = new Ext.ux.Html({
      width:60,
      value: '¿ ¿',
      name: 'LeftBar_coal',
      id: 'home_LeftBar_coal_id'
});

but when it arrived FF, the display becomes '? ?". I used Firebug to take a look at the javascript, it says

var LeftBarcoalHtml = new Ext.ux.Html({
width:60,
value: '? ?',
name: 'LeftBar_coal',
id: 'home_LeftBar_coal_id'
});

I tried playing with view -> character encoding -> Chinese Simplified (GB2312), but made no difference.

Is there something you need to declare to make the Chinese char show up in the browser properly?

note: happens here too. when I paste the Chinese char into the question, it looked good. Once submitted it becomes inverse ? like you see here.
Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Try \uABCD where abcd is the unicode value of your character

http://www.khngai.com/chinese/charmap/tbluni.php?page=0

example : value: '\u4EE0 \u4EEf',

Author

Commented:
I don't understand something.

when I copy/paste the Chinese characters, they are not in unicode form. They simply show up as Chinese characters. How do I tell what encoding is used?

to use your notation with \u, it would mean having to find the unicode representation from these Chinese characters, is that right? What api will allow me to do that? copied Chinese characters -> unicode
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
>How do I tell what encoding is used?
Use the meta tag but the best is to conserv utf-8 and use unicode values when needed

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

>to use your notation with \u, it would mean having to find the unicode representation from these Chinese characters, is that right?
right

>What api will allow me to do that?
I see a tools here :
http://weber.ucsd.edu/~dkjordan/resources/unicodemaker.html
Right click on the page, choose view source and check the gettem function

Author

Commented:
I found out more.

The characters are in GBK. I added that charset in the html. made no difference.  the problem is not there.

The characters are passed up in json. When I check the output in my IDE, I can see that the chinese characters are properly included in the json string. on the browser side, I use Firefox to view the received json string, and already it shows that the Chinese characters have been converted to non-sense string, like '?????". by the time the json gets decoded in the js lib, the values remains as non-sense.

Anyone know why Firebug would already have mis-interpreted the json string?
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
it's not firebug, it's the way your program don't use the same encoding
use UTF8 everywhere and this type of issue left, else use \uABCD
for example on server side with PHP :

echo json_encode($a, JSON_UNESCAPED_UNICODE);

http://php.net/manual/en/function.json-encode.php
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
everywhere, I mean :

browser(client) and database(server side)

Author

Commented:
hey, I found it.

I had to declare charset in the servlet response.

resp.setContentType("text/xml; charset=GBK");

Author

Commented:
Not a direct answer, but on the right track

Explore More ContentExplore courses, solutions, and other research materials related to this topic.