Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do Chinese characters in JSON cause "bad control character" error with JSON.parse?
    primarykey
    data
    text
    <p>I have a standard HTML5-type client/server set up. The server side is all Java, and the client side is JavaScript. Using ajax I send queries and receive replies. Up to now, I've had no problems with <code>JSON.parse(data)</code>. However, I have a new user who entered her last name using Chinese characters. This is causing a "JSON.parse: bad control character in string literal" error on the client side.</p> <p>The server builds a reply as follows (exception handling omitted):</p> <pre><code>JSONObject jsono = new JSONObject(); jsono.put("last_name", last_name); jsono.put("first-name", first_name); String response = jsono.toString(); </code></pre> <p>The client receives something like:</p> <pre><code>{"last_name":"Smith","first_name":"Bob"} </code></pre> <p>The reply is displayed on a web page which is set to <code> &lt;meta charset="utf-8"&gt;</code>:</p> <pre><code>var theResult = JSON.parse(data); $('#first_name').html(theResult.first_name); </code></pre> <p>This works just fine. However, for the Chinese user, the client receives</p> <pre><code>{"last_name":"唐","first_name":"Bob"} </code></pre> <p>and this causes the json.parse error. </p> <p>I've now started looking at other characters. For example, <code>Andr&eacute;s</code> does not cause an error, but also does not display properly. It looks like <code>Andr�s</code>.</p> <p>So, I'm clearly missing something. Could someone enlighten me where the problem lies (e.g., is it server side? client side? JavaScript? jquery? html?) and how to solve it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload