By default, any url whose extension is .json will go through the
generic.json view which looks like this:

{{
###
# response._vars contains the dictionary returned by the controller
action
###
try:
   from gluon.serializers import json
   response.write(json(response._vars),escape=False)
   response.headers['Content-Type']='text/json'
except:
   raise HTTP(405,'no json')
}}

The above view will convert native python objects (strings, integers,
doubles, lists, dictionaries, booleans, etc) into their JSON notation
and all will be converted into proper Javascript objects on the client
side (magic!).

Security-wise, it might be a good idea to decide which extensions each
controller function supports (you can figure out the extension the
user specified in "request.extension") and redirect to an error if
someone tries access with an extension you do not plan on supporting.

Also, it allows you to make a function that can react differently
depending on which extension is specified (ex: It might send the
default page if the extension is "html" and then send specific data
the user requests if the extension is "json").

If you want to really see what is happening when you make the requests
(what is sent, what is received), I found Chrome's integrated
developer tools and Firefox with Firebug extension to be invaluable
debugging tools.

On Feb 21, 9:50 pm, darkblue_b <[email protected]> wrote:
> thank you! will try shortly
>
> On Feb 21, 6:45 pm, Anthony <[email protected]> wrote:
>
> > On Monday, February 21, 2011 9:08:16 PM UTC-5, darkblue_b wrote:
>
> > > I guess I am still missing something..
> > > I wrote this, but, logged in, I get a msg that says "no json" and a
> > > blank screen
>
> > > ##-- controllers/data_hose.py
> > > from gluon.serializers import json
>
> > > @auth.requires_login()
>
> > > ##------------------------------------
> > > def get_data():
> > >   sample_data = { 'key1' : 'response 1' }
> > >   return sample_data
>
> > > def data_as_json():
> > >   stuff = get_data()
> > >   return json( stuff )
>
> >  @auth.requires_login() is a Python decorator, so you have to put it on the
> > line immediately before the function you want to decorate -- in this case:
>
> > @auth.requires_login()
> > def data_as_json():
> >   stuff = get_data()
> >   return json(stuff)
>
> > Seehttp://web2py.com/book/default/chapter/08#Decoratorsformore details.
>
> > Also, any function in your controller file that takes no arguments or
> > doesn't start with two underscores can be accessed via a URL request, so you
> > may not want a function like get_data() in your controller. If the sole
> > purpose of that function is to be called by other functions (such as
> > data_as_json), then you should either start it with two underscores (i.e.,
> > def __get_data()), make sure it takes at least one argument (e.g., def
> > get_data(dummy_arg)), or import it from a module. For more details, 
> > seehttp://web2py.com/book/default/chapter/04#Dispatching.
>
> > Anthony
>
>

Reply via email to