Google App Engine和Ajax:POST时出现错误代码405

时间:2012-12-11 09:28:33

标签: ajax google-app-engine

有人可以帮我解决这个问题吗?当我点击POST时,返回的数据应该更新内容div。但我总是得到" 405方法不允许"错误信息。

main.py:

# -*- coding: utf-8 -*-
#!/usr/bin/env python2.7

import webapp2
import os
from google.appengine.ext.webapp import template

class Main(webapp2.RequestHandler):
    def get(self):
        render(self, 'base.html', {})

class Form(webapp2.RequestHandler):
    def get(self):
        render(self, 'form.html', {'name': 'get'})
    def post(self):
        render(self, 'form.html', {'name': 'post'})

def render(handler, infile, template_values):
    path = os.path.join(os.path.dirname(__file__), 'templates/', infile)
    handler.response.out.write(template.render(path, template_values))

app = webapp2.WSGIApplication([
    ("/form", Form),
    ("/.*", Main)],
    debug=True)

base.html文件:

...
<body>

<div id="content">
{% include "form.html" %}
</div>

<script>
function submitForm() {
    $.ajax({
        type: "POST",
        url: $(this).attr('action'),
        data: $(this).serialize(),
        cache: false,
        success: function(returnData){
            $("#content").html(returnData);
        }
    });
}
</script>

</body></html>

form.html:

<form  id="submitForm" action="/form" enctype="multipart/form-data">
  name: <input type="text" name="abcd" />{{ name }}<br /><br />
      <input type="button" value="Submit" onclick="return submitForm()" />
</form>

1 个答案:

答案 0 :(得分:1)

问题解决了:只需将$(this)更改为$('#submitForm)