1

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.

17
  • 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. Commented Aug 14, 2020 at 21:04
  • "save arbitrary strings to the server and retrieve them back" is usually no big thing, assuming the server accepts the string encoding you use (utf8) and can store the values internally appropriately. You should not need a TextEncoder for 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. Commented Aug 14, 2020 at 21:07
  • @Bergi, Thanks for the edit and comments. I tried to send the strings with international characters in them within JSON object, but with 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. Commented Aug 15, 2020 at 20:59
  • 1
    Yes, the whole JSON text should be utf-8 encoded, and the Content-Type header should have the value application/json; charset=UTF-8. Make sure your server can correctly deal with that. Commented Aug 15, 2020 at 21:03
  • @Bergi, my code to send to the server is: $.ajax ({ type: 'POST', url: myUrl, data: {'item1' : 'str1', 'item2': 'str2', ..., 'internationalChars': encStr}, dataType: 'json', crossDomain: true, contentType:'application/json', beforeSend: ..., error: ..., success: ...}); Commented Aug 15, 2020 at 21:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.