I retrieve an encoded string (using TextEncoder into UTF-8, which was stringified before sending to the server) from the server using AJAX. I parse it upon retrieval and get an Object. I need to convert this Object to a decoded string. TextDecoder seems to have decode method, but it expects ArrayBuffer or ArrayBufferView, not Object. That method gives TypeError if I use my Object as-is:
var myStr = "This is a string, possibly with utf-8 or utf-16 chars.";
console.log("Original: " + myStr);
var encoded = new TextEncoder("UTF-16").encode(myStr);
console.log("Encoded: " + encoded);
var encStr = JSON.stringify(encoded);
console.log("Stringfied: " + encStr);
//---------- Send it to the server; store in db; retrieve it later ---------
var parsedObj = JSON.parse(encStr); // Returns an "Object"
console.log("Parsed: " + parsedObj);
// The following decode method expects ArrayBuffer or ArrayBufferView only
var decStr = new TextDecoder("UTF-16").decode(parsedObj); // TypeError
// Do something with the decoded string
This SO 6965107 has extensive discussion on converting strings/ArrayBuffers but none of those answers work for my situation. I also came across this article, which does not work if I have Object.
Some posts suggest to use "responseType: arraybuffer" which results in ArrayBuffer response from the server, but I cannot use it when retrieving this encoded string because there are many other items in the same result data which need different content-type.
I am kind of stuck and unable to find a solution after searching for a day on google and SO. I am open to any solution that lets me save "strings containing international characters" to the server and "retrieve them exactly as they were", except changing the content-type because these strings are bundled within JSON objects that carry audio, video, and files. Any help or suggestions are highly appreciated.
JSON.stringify(encoded)is really weird (and causing problems because it treats the buffer as an object not as an array). Why are you doing that? Send the ArrayBuffer, or at least a JSON array, to the server, which will make everything much easier.TextEncoderfor that. Can you please show your code that does send your content? Just putting strings in JSON should work. If it doesn't, chances are high that the server side needs fixing.contentType:application/json, server-side is unable to parse the JSON object when there are unencoded strings. Only way I found to bundle international character codes with other data in the same JSON object is to encode the string into utf-8. Changing the content type is not an option because of the other items. Even when omitting content type, the data is automatically encoded by AJAX using the default content type for the page.Content-Typeheader should have the valueapplication/json; charset=UTF-8. Make sure your server can correctly deal with that.$.ajax ({ type: 'POST', url: myUrl, data: {'item1' : 'str1', 'item2': 'str2', ..., 'internationalChars': encStr}, dataType: 'json', crossDomain: true, contentType:'application/json', beforeSend: ..., error: ..., success: ...});