[英]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. 我想将JavaScript XMLlhttpRequest发送到Google App Engine上的.py脚本。 But I am not looking for a
jQuery
solution 但我不是在寻找
jQuery
解决方案
HTML: 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: 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): 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 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)
POST http://myapp.appspot.com/communication.py 404(未找到)
Therefore, I guess there is some issue with finding communication.py 因此,我认为找到communication.py存在一些问题
Any ideas why the file is not found? 为什么找不到该文件的任何想法?
To sum up what was already suggested by Greg: 总结一下格雷格已经提出的建议:
Change handlers in app.yaml: 更改app.yaml中的处理程序:
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /communication
script: app.communication
- url: /.*
script: myapp.application
Changes in communication.py: 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.