3

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

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

the parse didn't work, maybe browser issue, and I just found out that apparently the string that was sent from the server lack JSON format, like missing the brackets,so the eval wasn't able to convert

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.