[英]What's the best way to send user-inputted text via AJAX to Google App Engine?
我正在Google App Engine(python sdk)中进行开发,并且我想使用jQuery发送Ajax请求来存储问题的答案。
将这些数据发送到服务器的最佳方法是什么? 目前我有:
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
为空。
该data
在您的ajax
调用必须包括所有要发送的数据-张贴几百个字符是绝对没有问题的,它是最肯定的推荐方法。
不要使用GET
代替POST
- 这 (我怀疑是你的“发送作为查询字符串”的意思),只会给你买各种各样的麻烦,同时也违反语义的GET
(应限于“只读”查询,没有副作用)。
您的数据格式错误:
data: {
question_id: question_id,
input_answer: $(answerInputControl).text()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.