JSON invalid token
Hello, I have a very annoying issue with my parse function. I'm receiving a JSON string (I'm most certain it is) from my websocket server. I have validated it online but still get a "invalid token" error when parsing it with my script.
However, if I put in into the function manually everything works perfect. What's wrong with my string?
function parseSocketMsg(msg){
var obj = jQuery.parseJSON(msg);
$.each(obj, function(i, item){
var rec = getRect(item.id);
rec.x = item.x;
rec.y = item.y;
});
}
function connect(){
var host = "ws://localhost:8080/scrabble/server.php";
try{
socket = new WebSocket(host);
print("Connected. Awaiting Handshake...");
socket.onopen = function(msg){
if(!ctx){
init();
}
print("Connection established - status "+this.readyState);
};
socket.onmessage = function(msg){ parseSocketMsg(msg.data); };
socket.onclose = function(msg){ print("Connection Lost! Reconnecting..."); connect(); };
}
catch开发者_如何学运维(ex){}
}
There was a similar question recently, the problem was that there was an unexpect null character at beginning and end of the string.
I recommend that you dump in hex the exact contents of what your are trying to parse, visual inspection doesn't always show up weirdness in character data.
You have var host = "ws://...
could this be the problem? Are you sure you receive anything? (log/print/whatever whatever you get). I suppose you are reading the logs in the server and assuming that your js is getting it.
精彩评论