Hasan A Yousef
Hasan A Yousef

Reputation: 24928

UTF-8 Arabic in JavaScript

I read this, but it did not solve my issue, and as it is old, I'm posting a new question.

I've the below code, that is recieving a SSE from GoLang server, but the Arabic script is not encoded probely:

<!DOCTYPE html>
<html lang="ar-AR">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
</head>

<body>
  <div id="msg"></div>
</body>
<script src="socket.js" charset="utf-8" type="text/javascript"></script>
</html>

And

var source = new EventSource("http://localhost:1234/sse/signal");
source.onmessage = function (event) {
    var message = event.data
    document.querySelector('#msg').innerHTML = message;
}

And sample output I get is:

data: {
    "IsFromMe": false,
    "IsGroup": false,
    "ID": "26DE3A6A0AEA98406D286E9C58C40114",
    "PushName": "إدارة قطو٠و حلا",
    "Timestamp": "2022-07-05T09:45:46+03:00",
}

The PushName above should be Arabic script!

Upvotes: 0

Views: 453

Answers (1)

Hasan A Yousef
Hasan A Yousef

Reputation: 24928

As mentioned by @slebetman in his comments, the correct answer is to send the data from the server as utf-8 which is done in my case as I'm sending data as streaming, by sending the Content-Type as text/event-stream; charset=utf-8

My code in the GoLang server became:

w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")

Upvotes: 1

Related Questions