在Google App Engine中构建Ajax表单

时间:2008-10-17 10:22:24

标签: jquery ajax google-app-engine

我有一个表单,当我点击提交按钮时,我想与服务器通信并从服务器获取某些内容以显示在同一页面上。一切都必须以AJAX方式完成。如何在Google App Engine中执行此操作?如果可能的话,我想在JQuery中做到这一点。

编辑:code.google.com/appengine/articles/rpc.html中的示例不适用于表单。


编辑:rpc程序doesn't work for form

3 个答案:

答案 0 :(得分:7)

您可以使用jquery Form plugin使用ajax提交表单。效果很好。

$('#myFormId').submit(function() {
    // submit the form
    $(this).ajaxSubmit();
    return false;
});

答案 1 :(得分:1)

我在Firebug中添加它,你应该会在控制台中看到你的ajax调用。如果您在打开该地址时遇到异常,那么您的Python代码就会出现问题。也许你没有正确映射你的网址?

答案 2 :(得分:1)

我之前在jQuery中做过这样的事情(不确定它是否是“最好的”方式,但它确实有效):

function jsonhandler(data) {
   // do stuff with the JSON data here
}

var doajax = function () {
    arr = Object();
    $("#form_id").children("input,select").each(function() { arr[this.name] = this.value;});
    $.getJSON("<page to call with AJAX>", arr, function (data) { jsonhandler(data);});
}

$(document).ready(function () {
    $("#submit_button_id").replaceWith("<input id=\"sub\" name=\"sub\" type=\"button\" value=\"Submit\">");
    $("#sub").click(doajax);
}

您可以将$ .getJSON替换为whichever jQuery AJAX function does what you want。如果您只想显示您正在呼叫的页面的输出,$ .get可能是您最好的选择。如果您在表单中输入和选择之外还有其他输入类型,则还需要将这些输入类型添加到子函数中。