Im having some troubles getting this ajax call to work. The ajax function pass a simple variable to my python controller and the controller returns the result of a query. Im working with Web2py framework. The error im getting: cannot concatenate ‘str’ and ‘NoneType’ objects. And this is the code:
Ajax Call:
<script>
function TestVolumen() {
var selectVal = $('#zonas :selected').val();
$.ajax({
data: selectVal ,
url: '/shell/default/ajax2',
type: 'POST',
cache: false,
dataType: 'string',
success: function(request) {
//console.log(request[0]);
//console.log(request[0].zona);
selectVal = $('#zonas :selected').val();
for (x=0;x<request.length;x++){
if (request[x].zona == selectVal ) {
//alert(request[x].promedio);
//alert(request[x].zona);
$('div#tabs .vol').html(request[x].promedio.toFixed(4));
}
}
}
});
}
</script>
And this is the python controller:
def ajax2():
import gluon.contrib.simplejson
zona = request.vars
queryvol = "select sells.product1 FROM site inner join zona on site.zone_number = zona.id inner join sells on sells.site = site.id WHERE zona.id =" + zona +" ;', as_dict = True"
vol=db.executesql(queryvol)
return gluon.contrib.simplejson.dumps(vol)
Thanks for the help!! I really appreciate it.
The Python error you see means that the value of
zonais simplyNone. I guess this is due to a fact that you make the AJAX request incorrectly. When making a request you should define the data in{ 'variable_name' : value }format. For server-side: note thatrequest.varsis gluon.storage.Storage dictionary-like object.So, to fix this – modify your AJAX request by removing
dataType: 'string',and settingdatato something like{ 'zona' : selectedVal }. Second – userequest.vars['zona']to get the value on the server-side. Third – before writing Python code any further, read an memorize PEP8 by heart!