你好, 我有一個非常惱人的問題,我的解析函數。我從我的websocket服務器收到一個JSON字符串(我確定它是)。我在線驗證了它,但在使用我的腳本解析時仍然出現「無效標記」錯誤。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字符串嗎? –
嘗試通過使用螢火蟲的console.log或簡單的警報記錄自己的輸入。 – TJHeuvel
這是收到的:[{「id」:7,「x」:637,「y」:151},{「id」:23,「x」:672,「y」:151},{ ID:10,「x」:733,「y」:416}] – Nokus