python - How to send a POST request with JavaScript XMLlhttpRequest to Google App Engine? -
I'm the one on Google App Engine. I would like to send the XML XMLhttpRequest to the XML script. but I jQuery
solution
HTML:
& lt; Form action = "javascript: SendMessage ();" & gt; & Lt; P & gt; User: & lt; / P & gt; & Lt; Input value = "" name = "user" id = "user" & gt; & Lt; Br> & Lt; B & gt; Comment here: & lt; / B & gt; & Lt; Div & gt; & Lt; Textarea id = "message" name = "message" rows = "3" cols = "60" & gt; & Lt; / Textarea & gt; & Lt; / Div & gt; & Lt; Input type = "submit" & gt; & Lt; / Form & gt; & Lt; Div id = "result" & gt; & Lt; / Div & gt;
Javascript:
& lt; Script type = "text / javascript" language = "javascript" & gt; Var url = window.location.href; Var element_id = "my_first_element"; Function sendMessage () {console.log ("working?"); Var xhr; If (window.XMLHttpRequest) {xhr = New XMLHttpRequest (); } And {xhr = new ActiveXObject ("Microsoft.XMLHTTP"); } Xhr.onreadystatechange = function () {if (xhr.readyState == 4 & amp; amp; xhr.status == 200} {document.getElementById ('Result'). InnerHTML = "status:" + xhr.status + "
Status Text:" + xhr.statustext + "
Server Response:
" + Xhr.responseText;}} Xhr.open ( Xhr.sendRequestHeader ("content-type", "app / x-www-form-urlxoded"); Xhr.send ("url =" + encodeuri (url) + "& Amp; user =" + encodeuri (document.getElementById ('user'). InnerHTML) + "& amp; Message =" + encodeurI (document.getElementById ('message'). WinnerHTML) + "& element_id = "+ Encodeuri (element_id));} & lt; / script>
Google App Engine (script file Is known as Communications and is in the home directory of my GAE app):
Category comments service (webapp2.RequestHandler): "" "This handler is responsible for the commenting service "" DRF post (self): comment = comments () user = cgi.sec (auto. Request) ('user')) message = cgi.space (auto) .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. Aliment_id = cgi.escape (self.request.get ('Element_id')) comment.put () self.response.out.write ("Input 1:% s
Input 2:% s" % (User, message))
app.yaml
Application: myapp version: 1 runtime: python27 API_version: 1 thread: correct handlers: - url : / Stylesheets static_dir: Stylesheets - url: /.* script: M.app.application library: - name: webapp2 version: latest - name: jinja2 version: latest
error message in console Found:
POST
/ P>/ Div>
For information already suggested by Greg:
Change the handlers in the app.yaml:
Handler: - url: / Stylesheets static_dir: stylesheets - url: / communication script: apcommuation- url: /.* script: map. Applications
Communications changes:
Class comments service (webapp2.RequestHandler): "" "This handler is responsible for the commenting service" "DRF 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 ("Input 1:% s <" Br / & gt; Input 2:% s "% (user, message)) app = webapp2.WSGIApplication ([('/ communication', comments service]], debug = true)
Comments
Post a Comment