3

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 ?

5
  • 1
    When calling .data() without parameters it returns an object with all the data-* attributes. And an object is not JSON. Commented Mar 2, 2016 at 18:29
  • Could you JSON.stringify it? Commented Mar 2, 2016 at 18:31
  • "command is json" .... no it's not. You are confusing javascript objects and json which is a string representation of those objects Commented Mar 2, 2016 at 18:34
  • Can you add a working jsFiddle so we can help you a little better? Commented Mar 2, 2016 at 18:51
  • @DuncanTidd yes, thats worked, thank you. I have answered for other people too Commented Mar 2, 2016 at 19:09

1 Answer 1

2

@Duncan Tidd was right. I just needed to stringify it.

    //html
    <button key1="1"
       data-key2="2"
       data-key3="3"
       data-key4="4"
    type="button"
    class="send btn btn-danger btn-lg">
    <span class="glyphicon glyphicon-off"></span></button>

     //javascript
     $(".send").click(function(evt){
                evt.preventDefault();

                var command = JSON.stringify($(this).data());
                console.log(command);
                ws.send(command);
        });


//console
{"key1":"1","key2":2,"key3":3,"key4":4}
Sign up to request clarification or add additional context in comments.

Comments

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.