1

您好,在测试 GET 和 POST 方法时,我不断收到 405 Method Not Allowed 该资源不允许使用 POST 方法。该计划是让用户发布姓名和昵称,并使用 ajax 保存然后显示输入。

import webapp2
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template

mainPage = """
   <html>
<head>
    <title>Pokemon List</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
    <script src="js.js"></script>

</head>


<body>

    <div data-role="header" data-theme="a">
        <h1>Pokemon App</h1>
 <div data-role="page" id="menu" class="info">
        <div data-role="header">
            <h3>Menu</h3>
            <a href="#" data-role="button" data-rel="back" id="backbutton" data-theme="c" data-icon="arrow-l">Back</a>
        </div>

        <div data-role="menuList">
            <ul id="actions2" data-role="listview" >

                <li><a href="#input" data-transition="none">Input Data</a></li>
                <li><a href="#data" data-transition="none">Check Data</a></li>

            </ul>
        </div>
    </div>

<div data-role="page" id="input" class="info">
    <div data-role="header" data-theme="a">
        <h1>Post data</h1>
        <a href="#input" data-role="button" data-rel="back"  id="backbutton2" data-theme="c" data-icon="arrow-l">Back</a>
    </div>
  <title>Name and Message</title>
</head>
<body>
    <h2>Input Name</h2>
        <form method='post'>
        <label for="name">Name</label>
        <input type='text' id="name" name="name" size="40" value="{{user}}"/><br/>
        <label for="nickname">Nickname</label>
        <textarea id="nickname" name="nickname" rows="6" cols="50"></textarea><br/>
        <input type="submit" value="Submit Button" />
    </form><br/>

    <table>
            {% for nickname in messages %}
                {{ nickname.asTableRow }}
            {% endfor %}
        </table>



    <div data-role="namelist" >
        <ul data-role="listview" data-split-icon="delete" data-split-theme="a" data-inset="true" class="list"  id="list">


        </ul>
    </div>

</div>
</div>
   </div>


        </body>
</html>

"""

class Message(db.Model):
    # This one has 3 properties...


    user = db.StringProperty()
    name = db.StringProperty()
    nickname = db.StringProperty()
    timestamp = db.DateTimeProperty(auto_now_add=True)


    def asString(self):
        return "%s user name: %s the nickname %s " %(self.user, self.name, self.nickname)

    def asJSONString(self):
        return '{"name": "%s", "nickname": "%s"}' % (self.user, self.name, self.nickname)


class MainPage(webapp2.RequestHandler):
    def get(self):
        usr = self.request.get('user')
        nme = self.request.get('name')
        nnme = self.request.get('nickname')
       # self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write(mainPage)

        if len(usr) > 0 and len(nnme) > 0:
            newmsg = Message(user=usr,name=nme,nickname=nnme)
            newmsg.put()
        #else:
            #self.response.out.write('Error in input')

def getJSONMessages(callback):
        messages = db.GqlQuery("SELECT * FROM Message ORDER BY timestamp")
        strlist = ""
        for message in messages:
            if len(strlist)>0:
                strlist += ',' + message.asJSONString()
            else:
                strlist = message.asJSONString()
        if callback =='':
            return '[' + strlist + ']'
        else:
            return callback+ '([' + strlist +']);'

class JSONInterface(webapp2.RequestHandler):

    def post(self):
        usr =self.request.get('usr')
        nnme =self.request.get('nnme')

        callback = self.request.get('callback')
        if len(usr) > 0 and len(nnme) >0:
            newmsg = Message(name=usr, nickname=nnme)
            newmsg.put()
        if len(usr)>0:
            self.response.out.write(getJSONMessages(callback))
        else:
            self.response.out.write("something didnt work")

    def get(self):
        callback = self.request.get('callback')
        self.response.out.write(getJSONMessages(callback))


app= webapp2.WSGIApplication([('/', MainPage),
                                      ('/json', JSONInterface)], debug=True)



def main():
    run_wsgi_app(app)

if __name__ == "__main__":
    main()

该应用程序成功部署到 Google App Engine。我检查了 App Engine 中的日志,但没有发现错误。有人可以告诉我的python代码是否有问题。我目前正在使用 javascript 处理一些 JSON,是否需要这样才能使 GET、POST 方法正常工作?感谢您的时间和帮助。

更新

如果我将 MainPage 请求处理程序更改为发布,我仍然会收到 405 方法错误。

4

1 回答 1

1

form你的方法中,post但在MainHandler你的方法中get,也许这就是你看到的原因405 method not allowed

于 2013-05-07T02:40:41.460 回答