the deal is basically that if you return a dict, web2py is compelled to pass it to the corresponding view, which in turns in most of cases returns html. There's nothing wrong in returning a dict and then having a generic "json" view that serializes that dict, and as a matter of fact, generic.json does just that. But if you return a string, you skip the "render the view" part.
On the parsing side: if you are looking for json, you should also return the correct content-type if you want libraries (e.g. jQuery) to parse for themselves. Either you force it with response.headers['Content-Type'] = 'application/json' or - my personal preference - you just ask for /a/c/f.json instead of /a/c/f . the correct content type will be set by web2py. On Wednesday, January 20, 2016 at 10:20:22 PM UTC+1, Ian W. Scott wrote: > > Thanks. So to clarify, if I serialize the return value as json in the > controller, then I can use it in javascript. It looks like I still have to > parse the json on the javascript end (like $.parseJSON(mydata)) but it > works like a charm. > > Ian > > On Wednesday, January 20, 2016 at 2:50:35 PM UTC-5, Niphlod wrote: >> >> from gluon.serializers import json >> >> def uh(): >> return json(blablabla) >> >> On Wednesday, January 20, 2016 at 8:45:55 PM UTC+1, Ian W. Scott wrote: >>> >>> I'm trying to use ajax calls to update the data for a chart dynamically. >>> I don't want to make the chart a component that refreshes, I just want to >>> get the controller return value back to the javascript in the view, so that >>> I can update the chart via javascript. But web2py's ajax function seems to >>> only (a) update a part of the page html, or (b) send page data to the >>> controller for use in the back-end. There doesn't seem to be any way to get >>> the controller's return value as a data object for the javascript to use. >>> >>> I've tried just using jquery's get() method like this: >>> >>> $.get(my_controller_url, function(data){ >>> console.log('got ajax data', data); >>> }); >>> >>> But if the controller's return value is a tuple there seems to be no >>> data sent back, or at least I don't know how to access it from the 'data' >>> variable in this example. If I make the controller a dictionary I get html >>> back. But I don't want html. I just want the data. >>> >>> So how can I do this? >>> >> -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.