Let me know if this is not what you're going for. Beginning web programming can be hard to conceptualize what's going to happen on the client and server side. hang in there!
import cherrypy
import os
import json
MEDIA_DIR = os.path.join(os.path.abspath("."), "media")
class AjaxApp(object):
@cherrypy.expose
def index(self):
# Sample page that displays the number of records in "table"
# Open a cursor, using the DB connection for the current thread
return """
<html>
<head>
<script lang="javascript">
function GetData()
{
// code for IE7+, Firefox, Chrome, Opera, Safari
if(window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
var someData_notJSON = JSON.parse(xmlhttp.responseText);
document.getElementById('Data').innerHTML = 'asdf: ' + someData_notJSON.asdf + ' and asdfw: ' + someData_notJSON.asdfw;
}
}
xmlhttp.open("GET","/submit222", true);
xmlhttp.send();
}
</script>
</head>
<body onload="GetData();">
<div id="Data">hi</div>
</body>
</html>
"""
@cherrypy.expose
def submit222(self):
# get data from db
res = { 'asdf': 1,
'asdfw' : 3 }
return json.dumps(res)
config = {'/media':
{'tools.staticdir.on': True,
'tools.staticdir.dir': MEDIA_DIR,
}
}
cherrypy.tree.mount(AjaxApp(), '/', config=config)
cherrypy.engine.start()
Hope this helps.