I get weird behavior when using parseInt ().
webSocket.onmessage = function (event) { var raw = event.data; alert(raw); var data = raw.split(":"); alert("lat:\"" + data[0] + "\" lon:\"" + data[1] + "\""); var x = parseInt(data[0]); var y = parseInt(data[1]); alert("lat:" + x + " lon:" + y); }
The first warning exits: 100: 100 is a string sent from the server.
Second warning outputs: lat: "100" lon: "100" is normal
However, the third warning output is: lat: 1 lon: NaN
What could be the reason for this?
UPDATE:
The problem was that server-side encoding generates some invisible unwanted characters. I updated the server code and the problem went away. Working solution .
source share