How to pass an array from jQuery to controller

1,200 views
Skip to first unread message
Message has been deleted
Message has been deleted

Noel Villamor

unread,
Aug 31, 2011, 1:17:13 AM8/31/11
to web2py-users
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) {
<< call controller here using .getJSON? >>
});
});
</script>

def mycontroller():
# Here, I wanted to receive xyz as an array.

Thanks for the help.

pbreit

unread,
Aug 31, 2011, 1:36:00 AM8/31/11
to web...@googlegroups.com
I think you might want the Ajax function:

Bruno Rocha

unread,
Aug 31, 2011, 2:18:09 AM8/31/11
to web...@googlegroups.com
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 <noe...@gmail.com> 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
[ 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 ]

Noel Villamor

unread,
Sep 4, 2011, 8:01:16 PM9/4/11
to web2py-users

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');



Anthony

unread,
Sep 4, 2011, 9:08:41 PM9/4/11
to web...@googlegroups.com
On Sunday, September 4, 2011 8:01:16 PM UTC-4, Noel Villamor wrote:

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');

If you set target to ':eval' and have the function return some javascript, the javascript will be executed upon return. See http://web2py.com/book/default/chapter/10#Eval-target.

Anthony 

Chris

unread,
Jul 2, 2014, 12:51:09 PM7/2/14
to web...@googlegroups.com
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. :)

Noel Villamor

unread,
Jul 2, 2014, 1:59:58 PM7/2/14
to web...@googlegroups.com
Nice one Chris and thanks for reviewing this post. Web2py has indeed come a long way from 2 years ago and is worthy of a revisit. Cheers!
Reply all
Reply to author
Forward
0 new messages