- 問(wèn)題:
- xhr.responseText/responseXML,服務(wù)器端, 響應(yīng)的并不是純數(shù)據(jù)艾猜, 而是夾帶著一些‘界面’,這樣不利于數(shù)據(jù)的重用捻悯。
- 引發(fā)問(wèn)題:
- 響應(yīng)純文本數(shù)據(jù)匆赃,不能表述每個(gè)字段所代表的含義。
數(shù)據(jù)交換格式(服務(wù)器端)
- 解決:自己定義數(shù)據(jù)編碼和解碼方式
- 存在問(wèn)題:不安全今缚,閱讀性差算柳,擴(kuò)展性差
XML(服務(wù)器端)
-
解決:
- 1.由于data中保存了我們所需要的數(shù)據(jù)
- 2.從data中拿到數(shù)據(jù),在creatXML中使用規(guī)定的格式給數(shù)據(jù)定義(也就是說(shuō)數(shù)據(jù)是怎么排版)荚斯。
include 'data.php';
<?php
include 'data.php';
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "<goods>\n"; //1
for($i = 0; $i < count($titles); $i ++) {
$xml .= "\t<good>\n"; //2
$xml .= "\t\t<title>"; //a
$xml .= $titles[$i];
$xml .= "</title>\n"; //a
$xml .= "\t\t<image>"; //b
$xml .= $images[$i];
$xml .= "</image>\n"; //b
$xml .= "\t</good>\n"; //2
}
$xml .= "</goods>"; //1
file_put_contents('goods.xml', $xml);
?>
- 3.用file_put_contents('goods.xml', $xml)方法埠居,將編寫(xiě)好的$xml放到goods里面。
在creatXML的頁(yè)面中使用這個(gè)方法事期,放到goods里面(此時(shí)goods里面的數(shù)據(jù)就是下面的樣子)
//例子:
<?xml version="1.0" encoding="utf-8"?>
<goods>
<good>
<title>滥壕。。</title>
<image>server/taobaoImg/兽泣。绎橘。.jpg</image>
</good>
</goods>
- 4.最后服務(wù)器端(01-goods-display-server)使用file_get_contents('goods.xml')方法獲取goods里面的內(nèi)容,然后響應(yīng)給客戶(hù)端唠倦。
<?php
header('Content-Type: text/xml; charset:utf-8');
// 獲取文件內(nèi)容
$xml = file_get_contents('goods.xml');
echo $xml;
?>
- 存在問(wèn)題:雖然可以描述和傳輸復(fù)雜數(shù)據(jù)称鳞,但是其解析過(guò)于復(fù)雜并且體積較大。