Thanks for that very helpful snippet Bruno.
Just a follow-up, will it be possible for the 'target' parameter below
be a jQuery function? I wanted to do some further processing on the
browser side when callback returns.
ajax("{{=URL('default','mycontroller')}}"+args ,[],'target');
On Aug 31, 6:18 pm, Bruno Rocha wrote:
> I dont know if this is the best approach, but I juste tested here and works.
>
> On Wed, Aug 31, 2011 at 2:17 AM, Noel Villamor wrote:
> > I wanted to pass an array from jQuery to a controller.
>
> >
> > var xyz= ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
> > $(function() {
> > $("body").click(function(event) {
>
> > ajax("{{=URL('default','mycontroller')}}"+"?array="+xyz,[],'target');
>
> });> });
> >
>
> > the above will call this url:
>
> /default/mycontroller?array=['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
>
> > def mycontroller():
> > # Here, I wanted to receive xyz as an array.
>
> myarray = eval(request.vars.array)
>
> the above will receive the string and evaluate as a Python list.
>
> (BUT, BE CAREFUL!! it can be used to crash your app)
>
> another solution may be better than the above, is to split the array as args
> and pass it separated.
>
>
>
> var xyz= ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
> args = xyz.join("/")
>
> $(function() {
> $("body").click(function(event) {
> ajax("{{=URL('default','mycontroller')}}"+args ,[],'target');
>
> });
>
> });
>
>
>
> the url will be called as
>
> /default/mycontroller/Sun/Mon/Tue/Wed/Thu/'Fri/Sat
>
> in controller
>
> def mycrontroller():
>
> array = request.args
> array[0] # "Sun"
>
> The second approach is better ans safe.
> --
>
> --
> Bruno Rocha
> [ About me:http://zerp.ly/rochacbruno]
> [ Aprenda a programar:http://CursoDePython.com.br]
> [ O seu aliado nos cuidados com os animais:http://AnimalSystem.com.br]
> [ Consultoria em desenvolvimento web:http://www.blouweb.com]