Ajax python如何发布数据
对不起,伙计们,也许以前有人问过这个问题。但我在谷歌上搜索了几天,仍然无法解决这个问题。我正在开发一个聊天系统,使用Google App Engine和Python。我希望用户输入她/他的消息并单击“提交”按钮。该操作将触发一个Ajax post函数“addMsg()”将消息发布到类Chat(URL:“/Chat”),该函数将消息添加到数据存储中。还有另一个Ajax函数“updateMsg()”,它将定期更新消息列表 代码可以很好地用于消息更新,但是,我无法正确地发布消息。有人能帮我吗?谢谢这是我的密码: chat.html: Chat.htmlAjax python如何发布数据,python,ajax,google-app-engine,Python,Ajax,Google App Engine,对不起,伙计们,也许以前有人问过这个问题。但我在谷歌上搜索了几天,仍然无法解决这个问题。我正在开发一个聊天系统,使用Google App Engine和Python。我希望用户输入她/他的消息并单击“提交”按钮。该操作将触发一个Ajax post函数“addMsg()”将消息发布到类Chat(URL:“/Chat”),该函数将消息添加到数据存储中。还有另一个Ajax函数“updateMsg()”,它将定期更新消息列表 代码可以很好地用于消息更新,但是,我无法正确地发布消息。有人能帮我吗?谢谢这是
<p>
<input type="text" name="message" size="60" /><br />
<input type="button" value="Submit" onclick="addMsg()" />
</p>
<div id="chatcontent"> </div>
<script>
function addMsg() {
var message = $('input[name=message]').val();
$.ajax({
type: "POST",
url: "/chat",
data: {'message': message},
cache: false
});
}
</script>
函数addMsg(){
var message=$('input[name=message]')。val();
$.ajax({
类型:“POST”,
网址:“/chat”,
数据:{'message':message},
缓存:false
});
}
{% for chat in chatlist %}
<p>
{{ chat.text }} ({{ chat.user.account }}) {{chat.created|date:"D d M Y" }}
</p>
{% endfor %}
# Called by URL "/chat"
class Chat(webapp2.RequestHandler):
def post(self):
message = self.request.get('message')
newchat = ChatMessage(user=self.session['userkey'], text=message, created=datetime.datetime.now())
newchat.put()
# Called by URL "/message"
class Message(webapp2.RequestHandler):
def get(self):
que = db.Query(ChatMessage).order('-created')
chatlist = que.fetch(limit=100)
render(self, 'message.html', {'chatlist': chatlist})
# Note: render() is a function to be imported, which is just a normal template rendering function. It works fine and is omitted here.
<p>
<input type="text" name="message" size="60" /><br />
<input type="button" value="Submit" onclick="addMsg()" />
</p>
<div id="chatcontent"> </div>
<script>
function addMsg() {
var message = $('input[name=message]').val();
$.ajax({
type: "POST",
url: "/chat",
data: {'message': message},
cache: false
});
}
</script>