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 badges1 Answer
Reset to default 4WebSocket 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条)