convert evt.data from websocket to jSON string or plain string in Javascript - Stack Overflow

Recently I played with websocket and it works great,in the client side with onmessage(evt) function, I

Recently I played with websocket and it works great,

in the client side with onmessage(evt) function, I received a message from the server side, the message is actually a JSON format like this:

{"Properties":{"name":"0a67d327-1f78-475e-b58a-d16706782223","publicname":"Page1"}}

then in the client side(html5 with javascript) I access the data using:

var page=evt.data;

then I access the JSON object

document.getElementById('name').innerHTML=page.Properties.name;

but it just won't work, I even use the eval function but it still doesn't work, I did check the page by using alert(page);

I wonder if the evt.data is not a string data but a byte, anyone have a solution for converting byte to string? or any other solution that may have something to do with this evt.data

Recently I played with websocket and it works great,

in the client side with onmessage(evt) function, I received a message from the server side, the message is actually a JSON format like this:

{"Properties":{"name":"0a67d327-1f78-475e-b58a-d16706782223","publicname":"Page1"}}

then in the client side(html5 with javascript) I access the data using:

var page=evt.data;

then I access the JSON object

document.getElementById('name').innerHTML=page.Properties.name;

but it just won't work, I even use the eval function but it still doesn't work, I did check the page by using alert(page);

I wonder if the evt.data is not a string data but a byte, anyone have a solution for converting byte to string? or any other solution that may have something to do with this evt.data

Share asked Mar 20, 2012 at 10:55 Eldon LesleyEldon Lesley 9356 gold badges20 silver badges39 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

WebSocket data is either string, Blob, or ArrayBuffer. In your case it is most likely a string so you need to parse it first:

var page = JSON.parse(evt.data);
console.log("Properties.name: " + page.Properties.name);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744819891a4595550.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信