Randy Tang
Randy Tang

Reputation: 4353

Google App Engine and Ajax: error code 405 when POST

Can somebody help me with this problem? When I clicked to POST, the returned data is supposed to update the content div. But I always got "405 Method Not Allowed" error message.

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>

Upvotes: 1

Views: 364

Answers (1)

Randy Tang
Randy Tang

Reputation: 4353

Problem solved: just change $(this) to $('#submitForm)

Upvotes: 1

Related Questions