node.js - Text encoding problems in JSON.stringified() object -


I have index.html, which sends a text to a PHP code This is a node by PHP pdf (curl) JS redirects again to the server, which is inserted in JSON message (UTF 8-encoded)

  // Node.js server file (APJS) - - Get JSN and it A & lt; Script & gt; To save it in the client JS render (index, {json: {string: "mystring"}} / Template to render (index.ejs) var data = & lt;% = JSON.stringify (json)% & Gt;;  

so that I can pass those variables into data in JSON. JSON is a big way out of here, I only wrote a part that creates a bug: The string present here creates "Inwald character" JS bug. What should I do ? Which encoding / decoding / escaping should I use?

I have UTF-8 everywhere, because all my other strings work, even with German or Arabic characters. In this special case, it is "mystring" below which breaks the app:

If I remove characters in red circles then it works.

Here's the string I got in the JSON:

  "Otto \ nTheater-, Konzert- und Gpb \ n \ u2028 \ u2028 Restore \ u00dfe 20 \ U2028 \ n51065 \ u00f6ln \ n \ nNote: 0000-000000-0 \ u2028 \ n Telefax: 0000-000000 \ n \ nEmail: address@mail.com \ u2028 " 

Because this is the text entered by the user, I should handle it, like the characters I do not have access to the PHP part of the code, only for nodes and client js. How can I find and remove these characters in JS?

  <% - JSON .stringify (data) .replace (/ [\ u0000 \ U00ad \ u0600- \ u0604 \ u070f \ u17b4 \ u17b5 \ u200c- \ u200f \ u2028- \ u202f \ u2060- \ u206f \ ufeff \ ufff0- \ uffff] / g, "\\ n")% & gt;  

I have replaced invalid unicode characters (which are valid for JSON but not in JS code) with line breaks. This fixes the problem


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -