The same controller method for ajax and nonajax call

22 views
Skip to first unread message

Alex

unread,
Nov 24, 2009, 11:06:06 PM11/24/09
to web2py-users
Hello,

I have "get_workout" action in controler and this action could be
called either directly using GET method, or could be called using
AJAX.

If it's called using GET it should return (render) whole page.
If it's called using AJAX it should return (render) only a part of the
page

It's not clear how to do it using web2py

I wanted to check inside controller method if current request it GET /
POST or AJAX and then change the logic of the method but didn't find a
way to check if current request is AJAX, how to do it?

I use such kind of link:
<a href="{{=URL('gym','default','get_workout')}}" onclick="ajax('{{=URL
(r=request,f='get_workout')}}', [], 'fitness_goals');return false;">
{{=a.title}}</a>

The base idea is to make partial page update using AJAX with loading
banner displaying during update and page opening using non-AJAX
request when whole page HTML will be loaded.

What is the best way to do it?

Thanks.

Thadeus Burgess

unread,
Nov 24, 2009, 11:09:08 PM11/24/09
to web...@googlegroups.com
On your ajax calls use the .json

so

http://mysite.com/init/default/index/ for normal pages

http://mysite.com/init/default/index.json/ to get the dict converted to json

or

index.xml to get it in xml format..

-Thadeus

Alex

unread,
Nov 24, 2009, 11:14:32 PM11/24/09
to web2py-users
But how can controller method know if current request is POST / GET or
AJAX?

Sometimes it's needed to generate very different part of HTML page and
return it to update some DIV on the page.

On Nov 25, 6:09 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> On your ajax calls use the .json
>
> so
>
> http://mysite.com/init/default/index/for normal pages
>
> http://mysite.com/init/default/index.json/to get the dict converted to json

mr.freeze

unread,
Nov 24, 2009, 11:42:14 PM11/24/09
to web2py-users
Send a var back with the ajax request:

<a href="{{=URL('gym','default','get_workout')}}" onclick="ajax
('{{=URL
(r=request,f='get_workout',vars=dict(is_ajax=True))}}', [],
'fitness_goals');return false;">
{{=a.title}}</a>

if request.vars.is_ajax:
....

On Nov 24, 10:14 pm, Alex <person...@gmail.com> wrote:
> But how can controller method know if current request is POST / GET or
> AJAX?
>
> Sometimes it's needed to generate very different part of HTML page and
> return it to update some DIV on the page.
>
> On Nov 25, 6:09 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
>
> > On your ajax calls use the .json
>
> > so
>
> >http://mysite.com/init/default/index/fornormal pages
>
> >http://mysite.com/init/default/index.json/toget the dict converted to json

Alex

unread,
Nov 25, 2009, 2:06:52 PM11/25/09
to web2py-users
Could work, but is not what I expected too.

Actually I found solution, if anyone will be intersted in it:

request.env.http_x_requested_with
could be str('XMLHttpRequest') on ajax request

request.env.request_method
is str('POST') or 'GET'

And this what I needed :)


On Nov 25, 6:42 am, "mr.freeze" <nat...@freezable.com> wrote:
> Send a var back with the ajax request:
>
> <a href="{{=URL('gym','default','get_workout')}}" onclick="ajax
> ('{{=URL
> (r=request,f='get_workout',vars=dict(is_ajax=True))}}', [],
> 'fitness_goals');return false;">
> {{=a.title}}</a>
>
> if request.vars.is_ajax:
>    ....
>
> On Nov 24, 10:14 pm, Alex <person...@gmail.com> wrote:
>
>
>
> > But how can controller method know if current request is POST / GET or
> > AJAX?
>
> > Sometimes it's needed to generate very different part of HTML page and
> > return it to update some DIV on the page.
>
> > On Nov 25, 6:09 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
>
> > > On your ajax calls use the .json
>
> > > so
>
> > >http://mysite.com/init/default/index/fornormalpages
>
> > >http://mysite.com/init/default/index.json/togetthe dict converted to json

Thadeus Burgess

unread,
Nov 25, 2009, 2:22:21 PM11/25/09
to web...@googlegroups.com
That'l work, thats awesome, thanks for sharing


-Thadeus

mr.freeze

unread,
Nov 25, 2009, 2:52:40 PM11/25/09
to web2py-users
Nice!
> > > >http://mysite.com/init/default/index.json/togetthedict converted to json
Reply all
Reply to author
Forward
0 new messages