0
我正在使用Google应用程序引擎制作网站。我想要显示来自Google SQL数据库的表格。我使用Jinja2从.py传递DB数据到.html。未捕获SyntaxError:从Jinja2加载JSON数据时出现意外的编号错误
但是我有一个问题:
Uncaught SyntaxError: Unexpected number" (line 88 in .html)
线88 $.get(function({{jsondata|safe}})
;完整的代码如下:
<script type="text/javascript">
var table;
$(function(){
$.get(function({{jsondata|safe}}){
var jsonobj = JSON.parse({{jsondata|safe}});
console.log(jsonobj);
table = $('#userindex').dataTable({
"sPaginationType": "bootstrap",
"sDom": "t<'row'<'col-xs-6 col-left'i><'col-xs-6 col-right'p>>",
"bStateSave": false
});
$.each(jsonobj, function(key,value){
table.fnAddData(value[0],value[1],value[2],value[3], value[4],value[5],value[6],value[7]);
});
});
});
</script>
我可以看到JSON数据时,我按下F12:
[[1, "u1", 27, "M", "ad1", "01043343883", "A34B", 0], [2, "u2", 24, "M", "ad2", "01099248819", "A35B", 0], [3, "u3", 0, "M", "ad3", "01043724865", "A36B", 0], [4, "u4", 0, "M", "ad4", "01043734865", "A37B", 0], [5, "u5", 24, "M", "ad5", "15555215554", "A38B", 4]]
这是我的Python代码:
cursor.execute("""select no, u_name, age, gender, U_adress, phone, car_num, penalty from User;""")
data=cursor.fetchall()
array_list=[]
for row in data:
temp = (row[0],str(row[1]),row[2],str(row[3]),str(row[4]),row[5],str(row[6]),row[7])
array_list.append(temp)
jsondata=json.dumps(array_list)
db.commit()
db.close()
template_values = { 'jsondata':jsondata }
template = JINJA_ENVIRONMENT.get_template('User.html')
self.response.write(template.render(template_values))
'函数({{jsondata |安全}})'是错误的,这是不是你将打印数据,这是参数列表的位置 –
它是['jQuery.get(url [,data] [,success] [,dataType])'](https://api.jquery.com/jquery.get/)而不是' jQuery.get(成功,...)' – Andreas
如果在页面加载时立即运行它,为什么要做'get'?在这种情况下做服务器端不是更好吗? – putvande