The title pretty much says it all.
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.
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?
My first guess was that it is a character encoding issue, but I'm not really sure.
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?
It's an AJAX POST request to a .dll served up by IIS 7, from a company called Magic Software.
Here's the response:
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
<h1>Max instance reached.</h1>
EDIT:
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:
http://jonisalonen.com/2012/from-utf-16-to-utf-8-in-javascript/
Convert integer array to string at javascript
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
Can someone look over this and tell me if it is an appropriate solution?
function UTF16toUTF8Str(str) {
var utf8 = [];
for (var i = 0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 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 & 0x3ff) << 10)
| (str.charCodeAt(i) & 0x3ff))
utf8.push(0xf0 | (charcode >> 18),
0x80 | ((charcode >> 12) & 0x3f),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f));
}
}
var i, str = '';
for (i = 0; i < 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;
}
EDIT
Here is the response from a HAR file that I got from Chrome's dev tools:
"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": "<\u0000h\u00001\u0000>\u0000M\u0000a\u0000x\u0000 \u0000i\u0000n\u0000s\u0000t\u0000a\u0000n\u0000c\u0000e\u0000 \u0000r\u0000e\u0000a\u0000c\u0000h\u0000e\u0000d\u0000.\u0000<\u0000/\u0000h\u00001\u0000>\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"
}
]
}
}