Anthony, thanks for your response. Please see the response below.
I was trying to do the following. Read data from a database and add it
to a table on the html page using ajax. I am using the standard ajax
functions available in jquery. The controller function sends the
database output in the form of json. I wanted to add this output to
the table. Currently, I am just using javascript to do so, but I
thought that for a more complicated scenario, it may be useful if I
could use python. In hindsight, I should have tried to see if python
can directly read the json format output sent by the controller
function. Please see the relevant controller function and the part of
the view (with some comments added to explain what I was trying to
do).
Relevant controller function that is called by the Ajax code
-------------------------------------------------------------------------
import simplejson as sjson
def returndatafromdb():
con, c = connecttodb()
c.execute('select * from results')
results = c.fetchall()
c.close()
con.close()
if len(results) > 0:
return sjson.dumps(dict(results=results))
Relevant part from the view
----------------------------------
<script type="text/javascript">
// Setting width of the table
$("#resultstable").attr('width', "300px");
// Aligning text to center
$("table").css("text-align", "center");
$.ajaxSetup({
cache: false
});
var loadurl = "{{=URL('default', 'returndatafromdb')}}";
var numrows = 0;
var getdata = function(){
$.post(
loadurl,
null,
function(serverresponse){ // serverresponse is in json format
var results = serverresponse.results;
// Instead of following javascript, I was wondering if
I could use
// web2py TR() and TD() helpers. But for that I need
to read "results"
// which is a javascript variable.
if (results.length > numrows) {
for (var i=0; i < results.length; i++) {
tablerow = "<tr><td>" + results[i][1] + "</td>" +
"<td>" + results[i][2] + "</td>" + "</tr>";
$(tablerow).appendTo("#resultstable");
};
};
numrows = results.length;
},
"json"
);
};
var checkdatabase = function(){
getdata();
setTimeout(checkdatabase, 6000);
}
$('#b1').click(checkdatabase);
==========================================================================
> --
>
>
>