【发布时间】:2011-07-13 12:56:51
【问题描述】:
你好, 我的解析函数有一个非常烦人的问题。我从我的 websocket 服务器接收到一个 JSON 字符串(我很确定它是)。我已经在线验证了它,但在用我的脚本解析它时仍然收到“无效令牌”错误。
但是,如果我手动输入该函数,则一切正常。我的弦有什么问题?
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){}
}
【问题讨论】:
-
你能发布一个示例 JSON 字符串吗?
-
尝试自己记录输入,使用 firebug 的 console.log 或简单的警报。
-
这是收到的:[{"id":7,"x":637,"y":151},{"id":23,"x":672,"y ":151},{"id":10,"x":733,"y":416}]
-
@Nokus:如果您使用的是 Chrome,有一个很好的资源可以查看实际收到的数据:chrome://net-internals/
标签: javascript json websocket