【问题标题】:How to send a POST request with JavaScript XMLlhttpRequest to Google App Engine?如何使用 JavaScript XMLhttpRequest 向 Google App Engine 发送 POST 请求?
【发布时间】:2014-09-18 18:46:08
【问题描述】:

我想向 Google App Engine 上的 .py 脚本发送一个 JavaScript XMLlhttpRequest。 但我不是在寻找jQuery 解决方案

HTML:

<form action="javascript:sendMessage();">
    <b>User:</b>
    <input value="" name="user" id="user">
    <br>
    <b>Comment here:</b>
    <div>
        <textarea id="message" name="message" rows="3" cols="60"></textarea>
    </div>
    <input type="submit">
</form>
<div id="result"></div>

JavaScript:

<script type="text/javascript" language="javascript">

    var url = window.location.href;
    var element_id = "my_first_element";

    function sendMessage() {
        console.log("working?");
        var xhr;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        else {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
            document.getElementById('result').innerHTML =
                "status: " + xhr.status +
                "<br />statusText: " + xhr.statusText +
                "<br />server response: <br />" + xhr.responseText;
            }
        }

        xhr.open("POST", "communication.py", true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.send("url=" + encodeURI(url) +
                 "&user=" + encodeURI(document.getElementById('user').innerHTML) +
                 "&message=" + encodeURI(document.getElementById('message').innerHTML) +
                 "&element_id=" + encodeURI(element_id)
                 );
    }

</script>

Google App Engine(脚本文件名为 communication.py,位于我的 GAE 应用的主目录中):

class CommentsService(webapp2.RequestHandler):
    """This Handler is responsible for the Commenting Service"""

    def post(self):

    comment = Comments()

    user = cgi.escape(self.request.get('user'))
    message = cgi.escape(self.request.get('message'))

    comment.url_ = cgi.escape(self.request.get('url'))
    comment.user_ = cgi.escape(self.request.get('user'))
    #comment.date = cgi.escape(self.request.get('date'))
    comment.message = cgi.escape(self.request.get('message'))
    comment.element_id = cgi.escape(self.request.get('element_id'))

    comment.put()

    self.response.out.write("input1: %s<br />input2: %s" % (user, message))

app.yaml

application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /.*
  script: myapp.application

libraries:
- name: webapp2
  version: latest
- name: jinja2
  version: latest

我在控制台中收到错误消息:

POST http://myapp.appspot.com/communication.py 404(未找到)

因此,我想查找communication.py 存在一些问题

任何想法为什么找不到文件?

【问题讨论】:

  • 你的 app.yaml 中有什么,你有 webapp2.WSGIApplication 路由 /communication.py 到你的处理程序吗?
  • 将 app.yaml 添加到问题中。是的,我得到了路由排序
  • 您的 app.yaml 正在向 myapp.application 发送请求 - 这是什么样的?最后有一个带有 .py 的 URL 在技术上是好的,但不寻常,这让我怀疑你希望它自动将 URL 与文件名匹配。这不是它的工作原理。
  • myapp.application 如果我没记错的话,GAE 默认需要。这是我的一个 .py 脚本(一开始就创建的第一个)。我想将表单数据发送到communication.py进行处理(数据库更新)。
  • 你错了,你可能应该查看入门指南来解释这种事情:developers.google.com/appengine/docs/python/…

标签: javascript python google-app-engine xmlhttprequest


【解决方案1】:

总结一下 Greg 已经提出的建议:

在 app.yaml 中更改处理程序:

handlers:
- url: /stylesheets
  static_dir: stylesheets
- url: /communication
  script: app.communication
- url: /.*
  script: myapp.application

communication.py的变化:

class CommentsService(webapp2.RequestHandler):
    """This Handler is responsible for the Commenting Service"""

    def post(self):
        comment = Comments()

        user = cgi.escape(self.request.get('user'))
        message = cgi.escape(self.request.get('message'))

        comment.url_ = cgi.escape(self.request.get('url'))
        comment.user_ = user
        #comment.date = cgi.escape(self.request.get('date'))
        comment.message = message
        comment.element_id = cgi.escape(self.request.get('element_id'))

        comment.put()

        self.response.out.write("input1: %s<br />input2: %s" % (user, message))

app = webapp2.WSGIApplication([('/communication', CommentsService)],
                              debug=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2020-11-03
    • 1970-01-01
    • 2018-09-03
    相关资源
    最近更新 更多