What's the best way to send user-inputted text via AJAX to Google App Engine?

Posted by Cuga on Stack Overflow See other posts from Stack Overflow or by Cuga
Published on 2010-05-25T03:28:09Z Indexed on 2010/05/25 3:31 UTC
Read the original article Hit count: 195

Filed under:
|
|
|

I'm developing in Google App Engine (python sdk) and I want to use jQuery to send an Ajax request to store an answer to a question.

What is the best way to send this data to the server? Currently I have:

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);
            }
        });
    }

This takes a question Id and provides it to the server via the query string. But on the server-side, I'm unable to access the content of the answer control which I want to store. Without Ajax, I'm able to perform this operation with the following:

class StoreAnswers(webapp.RequestHandler):
def post(self):
    question_id = self.request.get("question_id")
    answer_text = self.request.get("input_answer" + question_id)

But when doing this call through Ajax, my answer_text is empty.

  • Do I need to send the contents of this control as part of the data with the Ajax request?
  • Do I add the control itself to the query string? Its contents? Does it matter that the content might be a few hundred characters long? Is this the most-recommended practice?
  • If sending it as a query string, what's the best way to escape the content so that a malicious user doesn't harm the system?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about python