Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
html sending 'message' to python server logic?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Eric Jang  
View profile  
 More options Aug 17 2011, 2:27 am
From: Eric Jang <ericjang2...@gmail.com>
Date: Tue, 16 Aug 2011 23:27:59 -0700 (PDT)
Local: Wed, Aug 17 2011 2:27 am
Subject: html sending 'message' to python server logic?

Hello,

I have a bit of javascript (actually a web worker) on a web page compute
some math and postmessage the result back to the html page, with something
of this sort:
javascript:
document.getElementById(myanswer').textContent = someResult;

html:
<p>answer: <output id="myanswer"></output></p> and this works fine.

How can I get this 'myanswer' variable back to the Python backend so that it
can be written to the datastore?
according to
http://code.google.com/appengine/docs/python/tools/webapp/requestclas...
,
http://code.google.com/appengine/docs/python/gettingstarted/handlingf...,
html form data can be assigned an 'action' attribute, which is paired with a
handler on the python backend. However, in my case I don't want a form or
submit button, and I want the html to only submit the data once it is done
computing, and do this without the user having to do anything (like click
buttons).

Any way to do this?

Thanks!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NoseGAE  
View profile  
 More options Aug 17 2011, 10:17 am
From: NoseGAE <terzi...@unumoja.org>
Date: Wed, 17 Aug 2011 07:17:34 -0700 (PDT)
Local: Wed, Aug 17 2011 10:17 am
Subject: Re: html sending 'message' to python server logic?

JavaScript can be used to send an asynchronous request back to the webserver
using the XMLHttpRequest API (a/k/a Ajax). Most JavaScript libraries have
methods to simplify the creation of Ajax requests but the basics are quite
simple so here's how you might handle it without the help of a library:

function Ajax(data) {
var xmlhttp;
if (window.XMLHttpRequest) {
// for compliant browsers
xmlhttp = new XMLHttpRequest();

} else {

// IE5, IE6
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

 xmlhttp.onreadystatechange = function() {
// event handler to monitor the state of the request
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// what to do when the request is successfully completed
// you can use or discard the responseText
alert("Calculations sent back to webserver.\n" + xmlhttp.responseText);
}
}

// prepare and send the request
// third arg tells the request to process asynchronously so scripting
doesn't
// freeze up while waiting for the response from the webserver
xmlhttp.open("GET", "/put_in_datastore?data=" + escape(data), true);
xmlhttp.send();

}

And call this inline once your calculations return:

document.getElementById(myanswer').textContent = someResult;
Ajax(someResult);

As simple as it is, you're better off using a library like jQuery which
smooths out the differences between browsers and makes it easier to handle
errors or construct POST requests and the like.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Jang  
View profile  
 More options Aug 17 2011, 12:31 pm
From: Eric Jang <ericjang2...@gmail.com>
Date: Wed, 17 Aug 2011 09:31:16 -0700
Local: Wed, Aug 17 2011 12:31 pm
Subject: Re: [appengine-python] Re: html sending 'message' to python server logic?

Hi NoseGAE,

Thanks for the sample code.

So I tried using the following handler to process the Ajax URL, but it
didn't work:

class ResultsHandler(webapp.RequestHandler):
 def get(self):

self.response.out.write('you wrote:')

self.response.out.write(cgi.escape(self.request.get('content')))

def main():
    util.run_wsgi_app(webapp.WSGIApplication([
    (r"/", HomeHandler),
    ('/put_in_datastore', ResultsHandler)
    ]))

I don't think this is right though, what should the handler URL be for your
AJAX example?

Thanks!
- Eric


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NoseGAE  
View profile  
 More options Aug 17 2011, 2:05 pm
From: NoseGAE <terzi...@unumoja.org>
Date: Wed, 17 Aug 2011 11:05:49 -0700 (PDT)
Local: Wed, Aug 17 2011 2:05 pm
Subject: Re: [appengine-python] Re: html sending 'message' to python server logic?

Does the query string variable in the Ajax call match what's expected in the
get() method of your ResultsHandler? Your Python code is looks at a var
called 'content' and my example used 'data'. So either change your handler
or the JavaScript:

xmlhttp.open("GET", "/put_in_datastore?data=" + escape(data), true);

to

xmlhttp.open("GET", "/put_in_datastore?content=" + escape(data), true);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Jang  
View profile  
 More options Aug 17 2011, 2:28 pm
From: Eric Jang <ericjang2...@gmail.com>
Date: Wed, 17 Aug 2011 11:28:01 -0700
Local: Wed, Aug 17 2011 2:28 pm
Subject: Re: [appengine-python] Re: html sending 'message' to python server logic?

thanks NoseGAE! I got it to work, I verified this by writing to the
datastore :)

for some reason
self.response.out.write(cgi.escape(self.request.get('content'))) does not
work... is it because a template has already been written to the page or
something?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »