阅读背景:

如何将字符串列表发布到GAE

来源:互联网 

How to properly POST a list of strings with jQuery to Google App Engine app? I create something like this:

如何使用jQuery正确地将字符串列表发布到Google App Engine应用程序?我创建这样的东西:

$.post("/add", {tags:['first tag','second tag','third tag']}

And then in FireBug I see it becomes something like this:

然后在FireBug中我看到它变成这样:

tags%5B%5D=first tag&tags%5B%5D=second tag&tags%5B%5D=third tag

I am not sure I send a list properly. If it's fine, then how to process this list with Python? I try to use something like this:

我不确定我是否正确发送了一份清单。如果没关系,那么如何用Python处理这个列表呢?我尝试使用这样的东西:

tagsList = self.request.get("tags")

But without much success. Thanks

但没有太大的成功。谢谢

2 个解决方案

#1


0  

A straightforward solution would be to cast self.request.get("tags") in a list() however this doesn't work with me when I send a javascript array, but maybe it would work for your case

一个简单的解决方案是在列表中投射self.request.get(“tags”)(但是当我发送一个javascript数组时,这对我不起作用,但也许它适用于你的情况

#2


0  

You could serialize it into a JSON string, and this string will be treated as the post data.

您可以将其序列化为JSON字符串,并将此字符串视为发布数据。

$.ajax({
    type: "POST",
    url: "/add",
    contentType: "application/json",
    data: JSON.stringify({tags:['first tag','second tag','third tag']}),

Parses the data for example (in Java):

例如,解析数据(在Java中):

    try {
        reader = request.getReader();
    } catch (final IllegalStateException illegalStateException) {
        reader = new BufferedReader(new InputStreamReader(
                request.getInputStream()));
    }

    String line = reader.readLine();
    while (null != line) {
        sb.append(line);
        line = reader.readLine();
    }
    reader.close();

    String tmp = sb.toString();
    if (Strings.isEmptyOrNull(tmp)) {
        tmp = "{}";
    }

    return new JSONObject(tmp);

分享到: