Tornado does not process an AJAX POST request with JSON in it

807 views
Skip to first unread message

Norbert Markowicz

unread,
Sep 3, 2013, 1:56:51 PM9/3/13
to python-...@googlegroups.com
Hi guys,
 
I keep getting a no JSON object error in decoding a post requst in tornado:
...        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded
E 130903 13:49:33 web:1514] 500 POST /testjsonpost (169.37.195.11) 32.00ms
 
My JS and tornado codes are below. What gives?
 
JS:
var person = {
            name: "9798679",
            address: "gagg",
            phone: "bbbbbbbbbbbb"
        }
   $.ajax({
            url: '/testjsonpost',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                           alert("Success!");
            },
            data: person
        });
 
 
Tornado request handler:
class testJSONpost(tornado.web.RequestHandler):
    @asynchronous
    def post(self):
        data = json.loads(self.request.body)
        print data
        self.done()
 
Thanks in advance

Meir Kriheli

unread,
Sep 3, 2013, 3:23:51 PM9/3/13
to python-tornado
Please check what you're getting in request (body, arguments etc).

IIRC you should stringify the data. Something along the lines of:

$.ajax({
    type: "POST",
    url: "/testjsonpost",
    contentType: 'application/json',
    data: JSON.stringify(person),
    success: function (data) {
       alert("Success!");
    }
});

Some remarks:
  1. Note that dataType is for processing the server response.
  2. JSON.stringify may not be available on older versions of IE. See: http://caniuse.com/json
Cheers


--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

Norbert Markowicz

unread,
Sep 3, 2013, 4:28:27 PM9/3/13
to python-...@googlegroups.com
That did the trick Meir. Many thanks (as yes I agree i should have inspected the body contents myself)
 
Cheers
Reply all
Reply to author
Forward
0 new messages