admin管理员组文章数量:1310043
I have written a very simple test application that creates a websocket and parses the data it receives (server sends valid JSON data). The problem is that the first JSON object is parsed successfully, but all the subsequent objects are parsed with errors.
Here's all the code:
$("#connect").click(function ()
{
socket = new WebSocket("my server address");
socket.onopen = function ()
{
$("#log").append("Connection opened.<br/>");
socket.send(/* login information goes here */));
};
socket.onerror = function (e)
{
$("#log").append("Error: " + e.data + "<br/>");
};
socket.onclose = function ()
{
$("#log").append("Connection closed.<br/>");
};
socket.onmessage = function (e)
{
$("#log").append(index.toString() + ": " + e.data + "<br/><br/>");
console.log("Parsing " + index);
index++;
var obj = JSON.parse(e.data);
console.log("Parsed:");
console.log(obj);
};
});
What I'm getting is: The first time "socket.onmessage" called - JSON is parsed and JS console displays an object. When second one arrives it outputs it to my "log", but JSON.parse fails with error "Uncaught SyntaxError: Unexpected token ILLEGAL". What is puzzling me is that the string that is received is a valid JSON object - I have tested it through several JSON validators. I have even copy-pasted it from my "log" put it in a separate file and parsed it with $.getJSON - and it worked fine, no errors.
Browser: Chrome 13.0.782.112
Any ideas would be helpful.
Thank you.
I have written a very simple test application that creates a websocket and parses the data it receives (server sends valid JSON data). The problem is that the first JSON object is parsed successfully, but all the subsequent objects are parsed with errors.
Here's all the code:
$("#connect").click(function ()
{
socket = new WebSocket("my server address");
socket.onopen = function ()
{
$("#log").append("Connection opened.<br/>");
socket.send(/* login information goes here */));
};
socket.onerror = function (e)
{
$("#log").append("Error: " + e.data + "<br/>");
};
socket.onclose = function ()
{
$("#log").append("Connection closed.<br/>");
};
socket.onmessage = function (e)
{
$("#log").append(index.toString() + ": " + e.data + "<br/><br/>");
console.log("Parsing " + index);
index++;
var obj = JSON.parse(e.data);
console.log("Parsed:");
console.log(obj);
};
});
What I'm getting is: The first time "socket.onmessage" called - JSON is parsed and JS console displays an object. When second one arrives it outputs it to my "log", but JSON.parse fails with error "Uncaught SyntaxError: Unexpected token ILLEGAL". What is puzzling me is that the string that is received is a valid JSON object - I have tested it through several JSON validators. I have even copy-pasted it from my "log" put it in a separate file and parsed it with $.getJSON - and it worked fine, no errors.
Browser: Chrome 13.0.782.112
Any ideas would be helpful.
Thank you.
Share Improve this question asked Aug 19, 2011 at 1:30 stepanstepan 1,1052 gold badges8 silver badges14 bronze badges 5- 1 maybe you've already set the content type in your server? then the data does not need any more parsing. already happened to me, went nuts for a day for nothing – pif Commented Aug 19, 2011 at 1:47
- @pif hmm, I don't think that is the case here, the data is received via websocket. – stepan Commented Aug 19, 2011 at 2:01
- ahh ok... in my case anyway i'm using java servlets, then set the {response.setContentType("application/json")} and then tried to parse that like what you're doing and got Unexpexted token error thingy. like you i tried to copy and paste what browser is getting and it also is a valid json. when i tried NOT to parse it and access the data, works like a charm. – pif Commented Aug 19, 2011 at 2:27
- Have you tried just spitting out the output from 'e.data' (both first and second attempts) and paring them, possibly post them here. – Sunday Ironfoot Commented Aug 19, 2011 at 16:06
- @Sunday I have tried that. And I do believe it is a bug, since the solution to this problem was to use JSON.parse source code instead of actually calling JSON.parse. You can take a look at "2nd Update" here - stackoverflow./questions/7123908/… – stepan Commented Aug 21, 2011 at 14:32
2 Answers
Reset to default 3ES5 spec http://ecma262-5./ELS5_HTML.htm#Section_15.12.1 defines JSON whitespace as tab, cr, lf or sp. The Crockford skip-space uses the following code :
white = function () {
// Skip whitespace.
while (ch && ch <= ' ') {
next();
}
},
So if you have any spurious null characters or form-feeds etc in your response then the ES5 JSON parse will throw an error while the Crockford version will not.
You should do:
$.parseJSON( json );
see this for more info: http://api.jquery./jQuery.parseJSON/
本文标签: javascriptParse JSON received with WebSocket results in errorStack Overflow
版权声明:本文标题:javascript - Parse JSON received with WebSocket results in error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741823435a2399502.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论