Hello测试出GET和POST方法时我不断获得405方法不允许此资源不允许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方法错误。
答案 0 :(得分:1)
在form
中,您的方法为post
,但在MainHandler
中,您的方法为get
,也许这就是您看到405 method not allowed