UTF-8 Arabic in JavaScript - 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!

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")

Related

Unexpected token : Javascript jsp

I am novice in the JSP application, where we have to pass a url variable from servlet to JSP page using request.getattribute. While passing the data to the javascript function we are getting error below is
Uncaught SyntaxError: Unexpected token :
Please find below the JSP page.
<head>
<%String urlData=(String)request.getAttribute("urlDataFromServlet");%>
<script type="text/javascript">
var url=<%=urlData%>;
$(document).ready(function() {
var referLink = document.createElement("a");
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
Any help will be appreciated.
Thanks in advance.
You need to wrap this with quotes:
var url="<%=urlData%>";

node.js server and .html file

I started to work with node.js two days ago. I made my http server with node.js and I have a page which I made few month ago for some test purposes, but when I host my page on my computer it it works but without javascript files which are included in html page, like jQuery and some graph lib.
I tried to debug it with IE debugger, but I cannot solve that.
Funny thing is that when I open same page from my disk, just double clicking on .html file, it runs fine without any errors!
My question is what I supposed to do to make work? Did I miss something with node.js?
I have one html5 canvas element on page, and maybe that is a problem
This is my index.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello Dialog</title>
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta http-equiv="Cache-Control" content="no-store">
<meta http-equiv="cache-control" content="max-age=0">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT">
<meta http-equiv="pragma" content="no-cache">
<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="RGraph.line.js"></script>
<script type="text/javascript" src="RGraph.common.core.js"></script>
<script type="text/javascript" src="RGraph.drawing.background.js"></script>
<script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="./js/jquery-ui-1.9.2.custom.min.js"> </script>
<script type="text/javascript" src="index.js"></script>
<script type="text/javascript" src="temperature(1).json"></script>
</head>
<body style="font-size: 10px;">
<button id="btn_hello">Say Hello</button>
<button id="btn_hello1">LUKA</button>
<button id="btn_hello2">ANJA</button>
<button id="btn_hello3">MARKO</button>
<div id="dlg_hello">Hello World!</div>
<canvas id="graph" width="600" height="500"></canvas>
<script>
var data = [];
var limit;
var Temp = [];
var TimeStamp = [];
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
readTextFile("temperature(1).json", function(text){
data = JSON.parse(text);
for(var i =0; i<Object.keys(data).length; i++)
{
//alert(parseFloat(data[i].TimeStamp));
TimeStamp[i] = data[i].TimeStamp;
Temp[i] = parseFloat(data[i].Value);
}
DrawGraph(Temp, TimeStamp);
});
function DrawGraph(data, timestamp)
{
var line = new RGraph.Line({
id: 'graph',
data: data,
options: {
labels: timestamp,
gutterLeft: 55,
gutterRight: 35,
gutterBottom: 35,
gutterTop: 35,
title: 'A basic line chart',
backgroundGridColor: '#aaa',
backgroundGridDashed: true,
textAccessible: true,
scaleZerostart: true,
labelsOffsety: 5
}
}).draw();
}
</script>
</body>
</html>
and this is server.js in node.js
var http = require("http");
var fs = require("fs");
var windows1250 = require("windows-1250");
var ht = fs.readFileSync('./index.html',"utf-8");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html','Content-Length':ht.length});
res.write(ht);
res.end();
console.log(req.url);
}).listen(8888, function () {console.log("server is listening on port 8888");});
The server does not know where to find your javascript files. You might want to look into using express.js. This provides the capability to use the express.static command.
You are not hosting the files. The server doesn't know where to look for the JavaScript files, so it simply doesn't include them.
Look into using serve-static or a better solution to vanilla HTTP servers, express.
There is another similar answer here.
I remember in nodejs, you can read html like this:
var fs = require('fs');
fs.readFile('/path/html', 'utf8', function (err, data) {
if (err) {
console.log(err);
} else {
res.send(data);
}
});
if you want a better location path of the file, you can use
var path = require('path');
path.join(__dirname, '/path/html');
Then it will be like this:
var fs = require('fs');
var path = require('path');
var file = path.join(__dirname, '/path/html');
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log(err);
} else {
res.send(data);
}
});

Node.js socket.io with websocket

I'm begginer in Node.js or websocket. I have problem:
My HTML code:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
"use strict";
var gniazdo = new WebSocket('ws://localhost:3000');
gniazdo.onopen = function(){
console.log('Połączono');
};
gniazdo.onmessage = function(m){
console.log(m.data);
};
</script>
</body>
</html>
My Node.js code:
var io = require('socket.io')(3000);
io.on('connection', function(socket){
console.log('a user connected');
});
I have error in console:
WebSocket connection to 'ws://localhost:3000/' failed: Connection closed before receiving a handshake response
Help plz :)
Your client is using WebSockets, but Socket.IO has its own protocol (that may be transported over WebSockets, but it can also be transported over other protocols). Change your client to use Socket.IO's own client:
<script src="https://cdn.socket.io/socket.io-1.1.0.js"></script>
<script>
'use strict';
var gniazdo = io('ws://localhost:3000');
gniazdo.on('connect', function () {
console.log('Połączono');
gniazdo.on('message', function (m) {
console.log(m.data);
});
});
</script>

$.ajax passing multiple parameters

For some reason passing parameters using ajax is a real problem for me. I checked all of the questions regarding parameters and found the following as the correct way to pass parameters:
var username = $("#email_0000").val(),
password = $("#password_0000").val(),
func = 'emailexists',
bValid = true;
$.ajax({
type: "GET",
url: "php/ajax.php",
data: {
func:func,
email:username
},
success: function(data) {
alert(data);
}
});
The following is my php code:
<?php
$func = $_REQUEST['func'];
echo var_dump($_GET);
switch($func) {
case 'emailexists':
$email = $_REQUEST['email'];
echo 'Email address is='.$email;
break;
default:
return false;
break;
}
The alert is showing the following:
array(1){
["func"]=>
string(11)"emailexists"
}
Email address is=
I know I'm doing something wrong, but if someone has a clue I would appreciate it. Am I missing something in the "head" tag:
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="expires" content="1">
<!--Stylesheets-->
<link rel="stylesheet" href="css/jquery-ui.css">
<link rel="stylesheet" href="css/dhaStyle.css">
<!--JavaScripts -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script src="js/vendor/jquery.simplemodal.1.4.4.min.js"></script>
<script src="js/DHA_forms.js"></script>
Thanks in advance for your help, regards, Rich

What is wrong with this HTML, JavaScript and JSON code?

I have the following HTML code in a file called test.html. Both the HTML file and the JSON file below are stored on a server within the same directory.
<!DOCTYPE html>
<html>
<head>
<title>Shape Up</title>
<meta name="robots" content="noindex,follow">
<meta name="description" content="">
<meta name="keywords" content="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script language="JavaScript" type="text/javascript">
function ajax_get_json()
{
var hr = new XMLHttpRequest();
hr.open("GET", "game.json", true);
hr.setRequestHeader("Content-type", "application/json", true);
hr.onreadystatechange = function()
{
if(hr.readyState == 4 && hr.status == 200)
{
var data = JSON.parse(hr.responseText);
var results = document.getElementById("results");
results.innerHTML = "";
for(var obj in data)
{
results.innerHTML += data[obj].title;
}
}
}
hr.send(null);
results.innerHTML = "requesting...";
}
</script>
</head>
<body>
<div id="results"></div>
<script language="JavaScript" type="text/javascript">
ajax_get_json();
</script>
</body>
</html>
It pulls data from a file called game.json which is stored in the same directory.
{
"level_001":
{
"id":001,
"title":"Level 1",
"difficulty":0,
"comments":"this is how you complete level 1"
},
"level_002":
{
"id":002,
"title":"Level 2",
"difficulty":0,
"comments":"this is how you complete level 2"
}
}
The problem is that the results.innerHTML = ""; line is never reached. Why?
There are no errors in the browser, I've checked this on Firefox and on Safari.
According to jsonlint.com your JSON is invalid because of these properties:
"id":001
...
"id":002
You need to either remove the leading zeros:
"id":1
or make the numbers strings:
"id":"001"
For further details see the format rules spelled out at json.org
Presumably the line you mentioned is never reached because JSON.parse() gives an error about the above. (Do you not see an error in the browser's console?)

Categories

Resources