I am writing to get some help to "get unstuck". I can't figure out how to pass a dict as an argument to a controller in web2py.
Goal: I need to pass a html5 sessionStorage to a web2py controller to validate it and use it to generate order cart.
Here is what I have: I have a page that's a catalogue of products. I use html5 sessionStorage to collect the order. I looks like this:
{id:qty} e.g. {"1":"32", "15":"50"}
Then I have the following
jQuery('#cart').click(
function() {
alert("sending cart to server via ajax");
var order = JSON.stringify(sessionStorage);
console.log(order);
jQuery.ajax({
type: 'POST',
url: '/app/default/generateCart',
contentType: "application/json; charset=utf=8",
data: order,
dataType: 'json',
success: function(data) {alert('sent to server');}
});
});
This correctly sends data to the server where I use request.vars
So the question is: how can I pass this dict-like structure to the controller? I could use JS to generate the link and put a whole dict in it, but that seems insecure and looks like bad practice.
I did read the book, but I apologize in advance: I have a bit of mental block on this problem and cannot see beyond what I did, which is: (1) - I send ajax to the controller above: generateCart() - generateCart() saves request.args into session.order, which cart() controller tries to retrieve from. In cart() session.order is empty. I may not fully understand how session works. (2) - I tried making ajax to the same controller cart(), but that also does not work - it's empty
I would appreciate a kick in the butt on what the practice here and probably a book reference I missed. But key qn is: How do I pass a dict to a controller?
PS: Yes, I am novice in software design and web development. I am trying to follow docs, but I think I got stuck in my head (so to speak). Thank you.