Need help using web.input (or web.data) to parse JSON data from POST request (probably newbish question)

1,723 views
Skip to first unread message

lefou

unread,
Apr 23, 2011, 5:11:16 PM4/23/11
to web.py
I have some client side javascript posting data to my web.py server
that looks like this:

$.ajax({
type : 'POST',
url : '/graph/',
dataType : 'json',
data : {nodes: [{pie:1, cake:2}]}, //temporary data
for testing output
success : function(data){
console.log(data);
}
});

When my server receives the POST, and I have some code that looks
something like this:

class graph:
def POST(self):
graph1 = web.data()
graph2 = web.input()
print graph1
print graph2

Then my output looks like

graph1 : nodes%5B0%5D%5Bpie%5D=1&nodes%5B0%5D%5Bcake%5D=2
graph2: <Storage {'nodes[0][cake]': u'2', 'nodes': {}, 'nodes[0]
[pie]': u'1'}>

What I want to happen of course is for the data posted to be
deserialized so that my python dictionary has the same structure as it
did in javascript. However, web.input seems to flatten the whole
thing, and web.data encodes it in some form that I can't do anything
with. Now I'm sure I'm bastardizing some web standards by trying to
do what I'm doing, but is there anything I can do to keep web.py from
flattening/encoding it funky? Or another way to get the result I
want?

Thank you.

Nola Stowe

unread,
Apr 23, 2011, 9:51:05 PM4/23/11
to we...@googlegroups.com
I had similar problems to you, but I found that it didn't work to put dummy data like that in the ajax call ... so here's my ajax call 

        function save(id) {
          var data_in = {};
          data_in['item_id'] = id;
          data_in['id'] = other_uid;
          data_in['name'] = other_name;
          $$.post("/save", data_in, function(data_out) {
            $$("#result").html(data_out);
          });
        }

Build the structure in javascript, then just pass it in "data_in" variable. jQuery knows how to send it.

Then in the python:

    input = web.input()
    cursor.execute("SELECT * FROM users WHERE id = '" + input['id'] + "'")
    cursor.execute("INSERT INTO users(id, name) VALUES (%s, %s)", (user_id, input['name']))

    cursor.execute("INSERT INTO user_goal_association(user_id, goal_id, completed) VALUES (%s, %s, now())", (user_id, input['goal_id']))

I can access the structure with input[fieldname]
Good tool for debugging is to throw this in just before you have the problem

import pdb; pdb.set_trace()

Then look at the console where your server is running and you can poke around
http://docs.python.org/library/pdb.html



--
You received this message because you are subscribed to the Google Groups "web.py" group.
To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.




--
http://www.rubygeek.com - my blog
http://www.DevChix.com - boys can't have all the fun
Reply all
Reply to author
Forward
0 new messages