Note that there are some explanatory texts on larger screens.

plurals
  1. POAn HTTP POST response is returning '\u0' characters between letters, how do I remove those or parse them in JavaScript?
    text
    copied!<p>The title pretty much says it all.</p> <p>I'm sending an HTTP POST to a .dll provided to me. The response text contains information that I need to parse and display to the user in a human readable way. I knew the response, but my JavaScript was informing me that the response wasn't matching, but when I viewed the response text, it was clearly exactly the same.</p> <p>Well, when I looked a little closer and view the response using Chrome's dev tools, it shows that there are '\u0' characters after every letter. Is it an end-of-character, or some kind of terminating mark for each character?</p> <p>My first guess was that it is a character encoding issue, but I'm not really sure.</p> <p>Could anyone enlighten me as to what's really going on? How do I replace those characters so I can check for a substring in the response?</p> <p>It's an AJAX POST request to a .dll served up by IIS 7, from a company called Magic Software.</p> <hr> <p>Here's the response:</p> <pre><code>HTTP/1.1 500 Internal Server Error Cache-Control: private Content-Type: text/html Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Thu, 21 Nov 2013 23:51:46 GMT Content-Length: 60 &lt;h1&gt;Max instance reached.&lt;/h1&gt; </code></pre> <hr> <p><strong>EDIT:</strong></p> <p>I used the following function to convert the UTF-16 string that I was getting into UTF-8. It works for my purpose. I cobbled it together from two different sources:</p> <p><a href="http://jonisalonen.com/2012/from-utf-16-to-utf-8-in-javascript/" rel="nofollow noreferrer">http://jonisalonen.com/2012/from-utf-16-to-utf-8-in-javascript/</a><br> <a href="https://stackoverflow.com/questions/14028148/convert-integer-array-to-string-at-javascript">Convert integer array to string at javascript</a> </p> <p>I should have much better knowledge of character encodings, and I haven't read too much into what this does together. I'm going to do some reading. :P</p> <p>Can someone look over this and tell me if it is an appropriate solution?</p> <pre><code> function UTF16toUTF8Str(str) { var utf8 = []; for (var i = 0; i &lt; str.length; i++) { var charcode = str.charCodeAt(i); if (charcode &lt; 0x80) utf8.push(charcode); else if (charcode &lt; 0x800) { utf8.push(0xc0 | (charcode &gt;&gt; 6), 0x80 | (charcode &amp; 0x3f)); } else if (charcode &lt; 0xd800 || charcode &gt;= 0xe000) { utf8.push(0xe0 | (charcode &gt;&gt; 12), 0x80 | ((charcode &gt;&gt; 6) &amp; 0x3f), 0x80 | (charcode &amp; 0x3f)); } // surrogate pair else { i++; // UTF-16 encodes 0x10000-0x10FFFF by // subtracting 0x10000 and splitting the // 20 bits of 0x0-0xFFFFF into two halves charcode = 0x10000 + (((charcode &amp; 0x3ff) &lt;&lt; 10) | (str.charCodeAt(i) &amp; 0x3ff)) utf8.push(0xf0 | (charcode &gt;&gt; 18), 0x80 | ((charcode &gt;&gt; 12) &amp; 0x3f), 0x80 | ((charcode &gt;&gt; 6) &amp; 0x3f), 0x80 | (charcode &amp; 0x3f)); } } var i, str = ''; for (i = 0; i &lt; utf8.length; i++) { if (utf8[i] !== 0) str += '%' + ('0' + utf8[i].toString(16)).slice(-2); // only add non-null characters to the string } str = decodeURIComponent(str); return str; } </code></pre> <hr> <p><strong>EDIT</strong></p> <p>Here is the response from a HAR file that I got from Chrome's dev tools:</p> <pre><code> "response": { "status": 500, "statusText": "Internal Server Error", "httpVersion": "HTTP/1.1", "headers": [ { "name": "Date", "value": "Fri, 22 Nov 2013 03:35:59 GMT" }, { "name": "Cache-Control", "value": "private" }, { "name": "Server", "value": "Microsoft-IIS/7.5" }, { "name": "X-AspNet-Version", "value": "4.0.30319" }, { "name": "X-Powered-By", "value": "ASP.NET" }, { "name": "Content-Length", "value": "60" }, { "name": "Content-Type", "value": "text/html" } ], "cookies": [], "content": { "size": 60, "mimeType": "text/html", "compression": 0, "text": "&lt;\u0000h\u00001\u0000&gt;\u0000M\u0000a\u0000x\u0000 \u0000i\u0000n\u0000s\u0000t\u0000a\u0000n\u0000c\u0000e\u0000 \u0000r\u0000e\u0000a\u0000c\u0000h\u0000e\u0000d\u0000.\u0000&lt;\u0000/\u0000h\u00001\u0000&gt;\u0000" }, "redirectURL": "", "headersSize": 223, "bodySize": 60 }, "cache": {}, "timings": { "blocked": 0, "dns": -1, "connect": -1, "send": 0, "wait": 475.0000000349246, "receive": 1.500034297350794, "ssl": -1 }, "connection": "21740", "pageref": "page_127" } ] } } </code></pre>
 

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