我正在开发Google App Engine(python sdk),我想用jQuery发送一个Ajax请求来存储问题的答案。通过AJAX将用户输入的文本发送到Google App Engine的最佳方式是什么?
将此数据发送到服务器的最佳方式是什么?目前我有:
function storeItem(question_id) {
var answerInputControl = ".input_answer_"+question_id;
var answer_text = $(answerInputControl).text();
$.ajax({
type: "POST",
url: "store_answer.html",
data: "question="+question_id,
success: function(responseText){
alert("Retrieved: " + responseText);
}
});
}
这需要一个问题ID并通过查询字符串提供给服务器。但在服务器端,我无法访问我想要存储的答案控件的内容。没有Ajax,我能够用下面执行此操作:
class StoreAnswers(webapp.RequestHandler):
def post(self):
question_id = self.request.get("question_id")
answer_text = self.request.get("input_answer" + question_id)
但通过Ajax做这个电话时,我answer_text
是空的。
- 我是否需要将此控件的内容作为Ajax请求的数据的一部分发送?
- 是否将控件本身添加到查询字符串?它的内容?内容可能长达几百个字符是否重要?这是最推荐的做法吗?
- 如果将其作为查询字符串发送,那么转义内容的最佳方式是什么?以便恶意用户不会损害系统?
我提到的查询字符串,因为我想在Ajax调用“数据:‘这一行的问题=’+ question_id”是建立一个查询字符串,但我想我这是错误的。在通过ajax调用发送内容之前,通过什么方式来转义内容? – Cuga 2010-05-25 03:43:42