405 Method Not Allowed, Ajax Post on Appengine

appengine

Ok this is will be a Quick Head Shot post.

405 Method Not Allowed, The method POST is not allowed for this resource

If you are developing on Google Appengine, hopefully you will not get this kind of problem.

PROBLEM

I am doing simple Ajax Post to my App on Appengine (Python stack). From the same domain, so this is not a Cross Domain ajax problem in this case.

When I post data to server, then I got “405 Method Not Allowed” response, and spent almost 2 days of googling time without any good result.

Check out your URL routing patterns, once again.

    # My first routing config
    ...
    ...
    ('/<username >', UserHandler),
    ('/ajax-action', AjaxHandler),

See something wrong? yes!!. Check the order of your URL routing, ordering matter.

The problem is : my UserHandler doesn’t have POST request handler, but when I posting data to AjaxHandler it never reached at all because request are always processed by UserHandler due to its URL pattern.

My UserHandler will work on this conditions:

http://mydomain.com/ekaputra07
http://mydomain.com/boby90

# My Ajax handler URL (but will passed to UserHandler instead.)
http://mydomain.com/ajax-action

SOLUTION

Do correct ordering for URL routing patterns of your App, or it will bite you later :D.

just change it to:

    # My new routing config
    ...
    ...
    ('/ajax-action', AjaxHandler),
    ('/<username >', UserHandler),

Then Voila!.. problem solved. Always put URL pattern that will capture any phrase from the root of your URL to the last.

Although I understand this concept very much, but sometime when you are in rush, you might be missed it.

Thats it, I hope it helps others who got the same problem.

Leave a Reply

Your email address will not be published. Required fields are marked *