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);
}
def add_item():
data = gluon.contrib.simplejson.loads(request.body.read())
// Do insert to db
print data
return dict()<div>
<{{=A('Add Me')}}
</div>
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:
doPost({{=URL('default', 'add_item')}}, data);
doPost('{{=URL('default', 'add_item')}}', data);
function doPost(callback, data) {
$.post(callback, data, function(data){})
.done(function() { alert("Done!"); })
.fail(function() { alert("Failed!");})
}
function add_item(url, 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, data);
}<a href="" id="additem" onclick="add_item({{=URL('default', 'add_item')}}, {{item.id}}, 1);">Add Me</a>
<a href="" id="additem" onclick="add_item({{=URL('default', 'add_item')}}, {{item.id}}, 1);">Add Me</a>
<a href="" id="additem" onclick="add_item('{{=URL('default', 'add_item')}}', {{item.id}}, 1);">Add Me</a><script>
$(document).ready(function() {
$('#additem').click(function() {
var url = '{{=URL('default', 'add_item')}};
add_item(url, {{=item.id}}, 1);
}
</script>
<a href="" id="additem">Add Me</a>1. For some reason every click results in a javascript alert saying that it Failed, even though I can successfully do stuff with the data in the controller. Is there some sort of code I'm supposed to return through the controller to let jquery know that it's been successful?
2. To be safe, I'll sanitize the expected integers like so:def add_item():# get json data as 'data' ...item_id = int(data['test']['item_id'])other_data = int(data['test']['some_detail'])# Insert that item_id and other_data into db...But if I expected a string, how would I sanitize that string before using it to do something with the database? Is there a safe practice for this type of approach of getting data from json?
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?
1. For some reason every click results in a javascript alert saying that it Failed, even though I can successfully do stuff with the data in the controller. Is there some sort of code I'm supposed to return through the controller to let jquery know that it's been successful?
Does the add_item() function have an associated view? If not, it may be generating an error (when a function returns a dict(), web2py looks for an associated view to execute). Instead, you can just return nothing.
2. To be safe, I'll sanitize the expected integers like so:def add_item():# get json data as 'data' ...item_id = int(data['test']['item_id'])other_data = int(data['test']['some_detail'])# Insert that item_id and other_data into db...But if I expected a string, how would I sanitize that string before using it to do something with the database? Is there a safe practice for this type of approach of getting data from json?
function do Post(callback, data) {
$.post(callback, data).then(doSuccess, doFail);
function doSuccess(data) { alert('Success!'); }
function doFail(data) { alert('Failed!'); }
}