今天想把数据库里面的table用 jquery的 datatables 展现一下.前端我完全不懂,照着文档做,发现表格出不来,但是我ajax输出的格式和官方例子里面是一样的。为什么我本地会出不来表格呢
flask代码:
from flask import Flask
import json
import sqlite3
db = sqlite3.connect("/Users/min/Downloads/DataTables-1.10.4-trial/hostdb.db",check_same_thread = False)
cu = db.cursor()
cu.execute("select * from hostinfo")
result = cu.fetchall()
app = Flask(__name__)
@
app.route('/dt')
def dts():
d={}
d['recordsTotal'] = 26
d["recordsFiltered"] = 26
d['data'] = result
print d
return json.dumps(d)
app.run(debug=True,port =80)
html 的代码
$(document).ready(function() {
$('#example').dataTable( {
"processing": true,
"serverSide": true,
"ajax": "http://127.0.0.1/dt"
} );
} );
官方例子的输出:(我用chrome看的response)
{"draw":1,"recordsTotal":57,"recordsFiltered":57,"data":[["Airi","Satou","Accountant","Tokyo","28th Nov 08","$162,700"],["Angelica","Ramos","Chief Executive Officer (CEO)","London","9th Oct 09","$1,200,000"],["Ashton","Cox","Junior Technical Author","San Francisco","12th Jan 09","$86,000"],["Bradley","Greer","Software Engineer","London","13th Oct 12","$132,000"],["Brenden","Wagner","Software Engineer","San Francisco","7th Jun 11","$206,850"],["Brielle","Williamson","Integration Specialist","New York","2nd Dec 12","$372,000"],["Bruno","Nash","Software Engineer","London","3rd May 11","$163,500"],["Caesar","Vance","Pre-Sales Support","New York","12th Dec 11","$106,450"],["Cara","Stevens","Sales Assistant","New York","6th Dec 11","$145,600"],["Cedric","Kelly","Senior Javascript Developer","Edinburgh","29th Mar 12","$433,060"]]}
我的/dt的输出:
{"draw":1,"recordsTotal": 26, "recordsFiltered": 26, "data": [["web01.ejuops.com", "10.0.16.1", "datacenter", "webserver", "4c 32g 300g", "ghs55g"], ["web02.ejuops.com", "10.0.16.2", "datacenter", "webserver", "4c 32g 300g", "ghs65g"], ["web03.ejuops.com", "10.0.16.3", "datacenter", "webserver", "4c 32g 300g", "ghsu5g"], ["web04.ejuops.com", "10.0.16.4", "datacenter", "webserver", "4c 32g 300g", "ghsj5g"], ["web05.ejuops.com", "10.0.16.5", "datacenter", "webserver", "4c 32g 300g", "ghs45u"], ["web06.ejuops.com", "10.0.16.6", "datacenter", "webserver", "4c 32g 300g", "ghs45j"], ["web07.ejuops.com", "10.0.16.7", "datacenter", "webserver", "4c 32g 300g", "ghs4ng"], ["web08.ejuops.com", "10.0.16.8", "datacenter", "webserver", "4c 32g 300g", "ghs44g"], ["web09.ejuops.com", "10.0.16.9", "datacenter", "webserver", "4c 32g 300g", "ggs45g"], ["web010.ejuops.com", "10.0.16.10", "datacenter", "webserver", "4c 32g 300g", "bhs45g"], ["web011.ejuops.com", "10.0.16.11", "datacenter", "webserver", "4c 32g 300g", "nhs45g"], ["web012.ejuops.com", "10.0.16.12", "datacenter", "webserver", "4c 32g 300g", "jhs45g"], ["web013.ejuops.com", "10.0.16.13", "datacenter", "webserver", "4c 32g 300g", "khs45g"], ["web014.ejuops.com", "10.0.16.14", "datacenter", "webserver", "4c 32g 300g", "lhs45g"], ["web015.ejuops.com", "10.0.16.15", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web016.ejuops.com", "10.0.16.16", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web017.ejuops.com", "10.0.16.17", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web018.ejuops.com", "10.0.16.18", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web019.ejuops.com", "10.0.16.19", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web20.ejuops.com", "10.0.16.20", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web21.ejuops.com", "10.0.16.21", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web22.ejuops.com", "10.0.16.22", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web23.ejuops.com", "10.0.16.23", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web24.ejuops.com", "10.0.16.24", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web25.ejuops.com", "10.0.16.25", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web26.ejuops.com", "10.0.16.26", "datacenter", "webserver", "4c 32g 300g", "ghs45g"]]}
现在问题是,既然ajax的输出格式和官网一样的话,我用的是官方的html模版,就改了那个ajax地址,应该出来我要的数据,但是现在就是没出来效果
我用chrome debug我自己出不来表格的这个页面,在network里面发现ajax请求里的response没有数据,但是我把ajax请求的url贴到浏览器明明有数据.
哪位有做过这个东西的帮指点一下..