Hi all,
I'm trying to pass some data from javascript in the view back to the
controller using json. I'm unsure how to do this correctly. I created a
util.js:
function doPost(callback, data) {
$.post(callback, data, function(data){})
.done(function() { alert("Done!"); })
.fail(function() { alert("Failed!");})
}
function add_item(item_id, some_detail) {
var test = {};
test.item_id = item_id;
test.some_detail = some_detail;
var dat = {'test' : test};
var data = $.toJSON(dat);
doPost({{=URL('default', 'add_item')}}, data);
}
My default.py controller:
def add_item():
data = gluon.contrib.simplejson.loads(request.body.read())
// Do insert to db
print data
return dict()
My add button in the view:
<div>
<{{=A('Add Me')}}
</div>
But I'm not sure how to tie this all together. The anchor tag needs an
onclick which points to the function add_item() in util.js. The util.js
should execute that function and do a post to the web2py controller, which
would then do an insert into the database. Could someone help me out? Or is
this not the ideal way of handling javascript data -> web2py?
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.