yossi
yossi

Reputation: 3164

web2py - getting posted json data through jquery

I'm new to web2py, trying to alter an existing application.

I have a JSON object in my JS, which I want to send to the server.

My JS is:

post_data = {ios: [{k:"v"}, 
                   {k: "v"}, 
                   {k: "v"}]};
$.post("/url", post_data, function(data) {}, "json"); // used with 'json' and without, same results

I want to access this data in my controller. so there, I tried to use request.vars.ios and request.post_vars.ios, getting a None ...

What am I doing wrong?

(note: the data is transmitted, and if i try to dump the request.vars, I get something like
<Storage {'ios[1][ranges_colors]': '', 'ios[0] .... etc which contains the data)

Upvotes: 0

Views: 960

Answers (1)

elyase
elyase

Reputation: 40963

Try this, on the client:

$.ajax({
    type: 'POST',
    url: '/url.json',
    contentType: "application/json; charset=utf-8", 
    data: post_data,
    dataType: 'json',
    success:  function(data) {  alert('Data sent'); }
});

Then on the server:

data = gluon.contrib.simplejson.loads(request.body.read())

Upvotes: 2

Related Questions