[英]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.