Eval can be used to crash a Python app or hack into it, so *please don't 
use it*, as tempting as it may be!! 
(http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html)

I spent some time working on this and found that something like this is a 
decent way to do it:

<script>
//http://stackoverflow.com/questions/8890524/pass-array-to-ajax-request-in-ajax
info = [];
info[0] = 'hi';
info[1] = 'hello';


$.ajax({
 type: "POST",
 data: {info:info},
 url: "index.php",
 success: function(msg){
 $('.answer').html(msg);
 }
});

</script>


In web2py:


    def getRequestVars(name):
        varValue = request.vars[name + '[]']
        if type(varValue) == str:
            return [varValue] #check for single value
        else:
            return varValue


info = getRequestVars('info')

This approach is nice because you can use both web2py and jQuery's 
automatic array handling. :)


On Wednesday, August 31, 2011 2:18:09 AM UTC-4, rochacbruno 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 <[email protected] 
> <javascript:>> wrote:
>
>> I wanted to pass an array from jQuery to a controller.
>>
>> <script>
>> var xyz= ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
>> $(function() {
>>     $("body").click(function(event) {
>>           
>>  ajax("{{=URL('default','mycontroller')}}"+"?array="+xyz,[],'target'); 
>
>     });
>> });
>> </script>
>>
>> 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.
>
> <script>
>
> var xyz= ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
> args = xyz.join("/")
>
>  $(function() {
>      $("body").click(function(event) {
>             ajax("{{=URL('default','mycontroller')}}"+args ,[],'target'); 
>
>      });
> });
>
> </script>
>
> 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 ]
>
>

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to