AJAX & Python: Response text from python script is an object -
edited out silly mistake
i using ajax access python script, text python script & display on webpage.
my problem: response text "undefined" when should "bbbb"
i confused going wrong? python script incorrect (not handling ajax (?requests?) correctly), javascript or wsgi server made?
html & javascript:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script type="text/javascript"> <!-- function post( dest, params ) { var xmlhttp; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { return xmlhttp.responsetext; } } xmlhttp.open("post",dest,true); xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded"); xmlhttp.send( params ); } function ontest() { var response = post( "cgi/aaa.py", "email=blah" ); var output = document.getelementbyid( "bb" ); output.innerhtml = response; alert( response ); } --> </script> </head> <body> <p id="bb"> abcdef </p> <a href="javascript:ontest()">click it</a> </body> </html>
my python script:
import cgitb; cgitb.enable() import cgi import os input_data = cgi.fieldstorage() print "bbbb" #print "you said: " + input_data['email']
you getting undefined because did post , didn't wait response come back. alert
call should inside status change, have return xmlhttp.responsetext;
right now. remember, javascript functions (except alert
ironically) don't block.
Comments
Post a Comment