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).
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:
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.
On Wed, Aug 17, 2011 at 7:17 AM, NoseGAE <terzi...@unumoja.org> wrote: > 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:
> 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.
> To post to this group, send email to > google-appengine-python@googlegroups.com. > To unsubscribe from this group, send email to > google-appengine-python+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-appengine-python?hl=en.
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:
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?
On Wed, Aug 17, 2011 at 11:05 AM, NoseGAE <terzi...@unumoja.org> wrote: > 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:
> To post to this group, send email to > google-appengine-python@googlegroups.com. > To unsubscribe from this group, send email to > google-appengine-python+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-appengine-python?hl=en.