How To Receive Json Data With Python (server With Websockets) And Javascript (client-side)
I have a newbie question with receiving JSON data from Python working as a server. I need to tell you that the server is based on WebSockets and I'm sending JSON data from Python t
Solution 1:
You need to parse the message you received and attach it to the dom!
ws.onmessage = function (event) {
    try {
        var obj  = JSON.parse(event.data);
        document.getElementById("message-1").innerHTML = obj.first_name;
        document.getElementById("message-2").innerHTML = obj.second_name;
        document.getElementById("message-3").innerHTML = obj.titles.join(" ");
    } catch {
        console.log("Object is not received, Message is:: ", event.data);
    }
}
If this is not working, then check the json format your are sending!
Remember JSON Needs to be valid json, Replace
'(single-quote) with"(double-quote):
d = {
    "first_name": "Guido",
    "second_name": "Rossum",
    "titles": ["BDFL", "Developer"]
}
Post a Comment for "How To Receive Json Data With Python (server With Websockets) And Javascript (client-side)"