I'm building a website with Tornado Websocket and Tornado Websocket accepts this type of json:
{"key1":1,"key2":2,"key3":3}
I want to papulate element attributes in json and send it to websocket. My javascript:
$(".send").click(function(evt){
evt.preventDefault();
var command = $(this).data();
console.log(command);
ws.send(command);
});
command is json but my websocket does not accept it and throws error when I try
#python
json_data = json.loads(message)
Error:
03-02 18:09 tornado.application ERROR Uncaught exception in /ws
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/websocket.py", line 417, in _run_callback
callback(*args, **kwargs)
File "/var/www/scripts/py/realtime.py", line 102, in on_message
kk = json.loads(message)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
I think that is because I don't need to send a json object, just a string with this syntax like above? Maybe I am wrong, I do not know. Can I convert my $(this).data(); json to the syntax like above or would it be better to produce a string from $(this).data(); and if yes how ?
.data()without parameters it returns an object with all thedata-*attributes. And an object is not JSON.JSON.stringifyit?