W3Data是什么?
W3Data 是一個(gè)將服務(wù)器數(shù)據(jù)帶到 HTML 應(yīng)用程序的工具。
W3Data 的目的是向 w3.js 提供來(lái)自 web 服務(wù)器的數(shù)據(jù)。
應(yīng)用程序模型
W3Data 使用應(yīng)用程序模型(用 JSON 編寫)來(lái)描述服務(wù)器應(yīng)用程序。
這個(gè)簡(jiǎn)單的模型描述了一個(gè)從數(shù)據(jù)庫(kù)中檢索數(shù)據(jù)的完整應(yīng)用程序:
model_customers.js
{"database" : {
"connection" : "localmysql",
"sql" : "SELECT * FROM Customers"}}
模型存儲(chǔ)在服務(wù)器上,不能由 web 用戶編輯。
您必須是服務(wù)器管理員或被賦予在服務(wù)器上編輯文件的權(quán)限的用戶。
使用應(yīng)用程序模型很簡(jiǎn)單,只需在調(diào)用 w3Http( ) 時(shí)將模型名稱添加到 w3data.php:
實(shí)例
<script>
w3.getHttpObject("w3data.php%3Fmodel%3Dmodel_customers", myFunction);
function myFunction(myObject) {
w3.displayObject("id01", myObject);}
</script>
嘗試一下 ?
從文本文件中顯示
實(shí)例
<script>
w3.getHttpObject("w3data.php%3Fmodel%3Dmodel_cd_from_txt", myFunction);
function myFunction(myObject) {
w3.displayObject("id01", myObject);}
</script>
嘗試一下 ?
這是應(yīng)用程序中使用的模型:
model_cd_from_txt
{"data" : {
"type" : "csvfile",
"filename" : "cd_catalog.txt",
"items" : [
{"name" : "title", "index" : 1},
{"name" : "artist", "index" : 2},
{"name" : "price", "index" : 5}]}}
這是逗號(hào)分隔的文本文件:
cd_catalog.txt
Empire Burlesque,Bob Dylan,USA,Columbia,10.90,1985
Hide your heart,Bonnie Tyler,UK,CBS Records,9.90,1988
Greatest Hits,Dolly Parton,USA,RCA,9.90,1982
Still got the blues,Gary Moore,UK,Virgin records,10.20,1990
Eros,Eros Ramazzotti,EU,BMG,9.90,1997
One night only,Bee Gees,UK,Polydor,10.90,1998
Sylvias Mother,Dr.Hook,UK,CBS,8.10,1973
Maggie May,Rod Stewart,UK,Pickwick,8.50,1990
顯示 JSON 文件
實(shí)例
<script>
w3.getHttpObject("w3data.php%3Fmodel%3Dmodel_cd_from_txt", myFunction);
function myFunction(myObject) {
w3.displayObject("id01", myObject);}
</script>
嘗試一下 ?
這是應(yīng)用程序中使用的模型:
model_cd_from_json.js
{"data" : {
"type" : "jsonfile",
"filename" : "cd_catalog.js",
"record" : "cd"
"items" : [
{"name" : "title", "nodename" : "title"},
{"name" : "artist", "nodename" : "artist"},
{"name" : "price", "nodename" : "price"}]}}
這是 JSON 文件:
cd_catalog.js
{"cd" : [
{ "title" : "Empire Burlesque", "artist" : "Bob Dylan", "price" : "10.90" },
{ "title" : "Hide your heart", "artist" : "Bonnie Tyler", "price" : "9.90" },
{ "title" : "Greatest Hits", "artist" : "Dolly Parton", "price" : "9.90" },
{ "title" : "Still got the blues", "artist" : "Gary Moore", "price" : "10.20" },
{ "title" : "Eros", "artist" : "Eros Ramazzotti", "price" : "9.90" },
{ "title" : "One night only", "artist" : "Bee Gees", "price" : "10.90" },
{ "title" : "Sylvias Mother", "artist" : "Dr.Hook", "price" : "8.10" }]}
從XML文件中顯示
實(shí)例
<script>
w3.getHttpObject("w3data.php%3Fmodel%3Dmodel_cd_from_txt", myFunction);
function myFunction(myObject) {
w3.displayObject("id01", myObject);}
</script>
嘗試一下 ?
這是應(yīng)用程序中使用的模型:
model_cd_from_xml
{"data" : {
"type" : "xmlfile",
"filename" : "cd_catalog.xml",
"record" : "CD",
"items" : [
{"name" : "artist", "nodename" : "ARTIST"},
{"name" : "title", "nodename" : "TITLE"},
{"name" : "country", "nodename" : "COUNTRY"}]}}
這是XML文件:
cd_catalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<PUBLISHED>1985</PUBLISHED>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<PUBLISHED>1988</PUBLISHED>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<PUBLISHED>1982</PUBLISHED>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary Moore</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin records</COMPANY>
<PRICE>10.20</PRICE>
<PUBLISHED>1990</PUBLISHED>
</CD>
<CD>
<TITLE>Eros</TITLE>
<ARTIST>Eros Ramazzotti</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>BMG</COMPANY>
<PRICE>9.90</PRICE>
<PUBLISHED>1997</PUBLISHED>
</CD>
<CD>
<TITLE>One night only</TITLE>
<ARTIST>Bee Gees</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Polydor</COMPANY>
<PRICE>10.90</PRICE>
<PUBLISHED>1998</PUBLISHED>
</CD>
<CD>
<TITLE>Sylvias Mother</TITLE>
<ARTIST>Dr.Hook</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS</COMPANY>
<PRICE>8.10</PRICE>
<PUBLISHED>1973</PUBLISHED>
</CD>
</CATALOG>
下載
下載該文件:https://7npmedia.w3cschool.cn/w3data.php.txt
將該文件復(fù)制到您的網(wǎng)站,并將其重命名為 w3data.php。
更多建議: