How to post a list of strings to GAE
How to properly POST a list of strings with jQuery to Google App Eng开发者_开发技巧ine app? I create something like this:
$.post("/add", {tags:['first tag','second tag','third tag']}
And then in FireBug I see it becomes something like this:
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:
tagsList = self.request.get("tags")
But without much success. Thanks
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
You could serialize it into a JSON string, and this string will be treated as the post data.
$.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):
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);
精彩评论