简体   繁体   中英

How to send a POST request with JavaScript XMLlhttpRequest to Google App Engine?

I would like to send a JavaScript XMLlhttpRequest to a .py script on Google App Engine. But I am not looking for a jQuery solution

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 (script file is called communication.py and is in home directory of my GAE app):

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

I got the error messsage in the console:

POST http://myapp.appspot.com/communication.py 404 (Not Found)

Therefore, I guess there is some issue with finding communication.py

Any ideas why the file is not found?

To sum up what was already suggested by Greg:

Change handlers in app.yaml:

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

Changes in 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM