如何通过 $.post 将列表值传递给 webapp2 后端?

How to pass a list value to a webapp2 backend via $.post?

在我的 coffeescript 前端中,我尝试将值列表传递给后端

data = {
  id: [2, 3, 4]
}

$.post url, data

在 Google 应用引擎 (python) 后端的处理程序中,我这样读取值:

    id_value = self.request.get('id')

    LOG.info("%s", id_value)

它总是只打印出“2”。

如何让后端获取列表[2,3,4]

$.post 默认以 url 编码格式发送数据,它以自己的方式处理嵌套结构。

您可能需要在发送前对 JSON 中的数据进行编码,然后在服务器端对其进行解码 - 例如 here.

The request object provides a get() method that returns values for arguments parsed from the query and from POST data.

If the argument appears more than once in a request, by default get() returns the first occurrence. To get all occurrences of an argument that might appear more than once as a list (possibly empty), give get() the argument allow_multiple=True.

因此,您应该使用类似于以下代码段的内容。您可以找到更多详细信息 here.

id_value = self.request.get('id', allow_multiple=True)

如果您需要访问请求正文中编码的变量 url(通常是使用 application/x-www-form-urlencoded 媒体类型提交的 POST 表单),您应该使用类似这样的方法.

id_value = self.request.POST.getall('id')