I have a jQuery post something like
var arr = ['some', 'string', 'array'];
jQuery.post('saveTheValues', { 'values': arr },
function(data)
{
//do stuff with the returned data
}, 'json'
);
And it goes to a cheerypy function:
@cherrypy.expose
def saveTheValues(self, values=None):
#code to save the values
But running the javascript returns 400 Bad Request
because Unexpected body parameters: values[]
.
How can I send an array to cherrypy?
The problem is that newer jQuery versions send the braces as part of the name which CherryPy doesn't like. One solution is to catch this on the CherryPy side:
@cherrypy.expose
def saveTheValues(self, **kw):
values = kw.pop('values[]', [])
#code to save the values
Another solution is to let jQuery use the traditional method of sending params by serializing the params with the traditional flag set to true. The following works with the CherryPy code unaltered:
var arr = ['some', 'string', 'array'];
jQuery.post('saveTheValues', $.param({'values': arr}, true),
function(data)
{
//do stuff with the returned data
}, 'json');