我有一个类似 jQuery 的帖子
var arr = ['some', 'string', 'array'];
jQuery.post('saveTheValues', { 'values': arr },
function(data)
{
//do stuff with the returned data
}, 'json'
);
然后它进入一个 cheerypy 函数:
@cherrypy.expose
def saveTheValues(self, values=None):
#code to save the values
但是运行 javascript 返回 400 Bad Request
因为 Unexpected body parameters: values[]
。
如何将数组发送到 cherrypy?
最佳答案
问题是较新的 jQuery 版本将大括号作为 CherryPy 不喜欢的名称的一部分发送。一种解决方案是在 CherryPy 端捕获它:
@cherrypy.expose
def saveTheValues(self, **kw):
values = kw.pop('values[]', [])
#code to save the values
另一种解决方案是让 jQuery 使用传统的发送参数的方法,方法是将传统标志设置为 true 来序列化参数。以下内容适用于未更改的 CherryPy 代码:
var arr = ['some', 'string', 'array'];
jQuery.post('saveTheValues', $.param({'values': arr}, true),
function(data)
{
//do stuff with the returned data
}, 'json');
关于jquery - 如何将 javascript 数组发送到 cherrypy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8065913/