1

有人可以帮我解决这个问题吗?当我点击 POST 时,返回的数据应该会更新内容 div。但我总是收到“405 Method Not Allowed”错误消息。

主要.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>

表单.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>
4

1 回答 1

1

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

于 2012-12-12T01:34:15.637 回答