本文實(shí)例講述了php+Ajax處理xml與json格式數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
一、ajax如何處理xml數(shù)據(jù)格式
register.php
只需修改上一篇《php+Ajax無(wú)刷新驗(yàn)證用戶名操作》中chuli函數(shù)部分
functionchuli(){
// window.alert("cuhli函數(shù)被調(diào)用"+myXmlHttpRequest.readyState);
//我要取出從register.php返回的數(shù)據(jù)
if(myXmlHttpRequest.readyState==4){
//------------看看如何取出xml數(shù)據(jù)--------
//獲取mes節(jié)點(diǎn)
var mes=myXmlHttpRequest.responseXML.getElementsByTagName("mes");
//取出mes節(jié)點(diǎn)值
var mes_value=mes[0].childNodes[0].nodeValue;
$("myres").value=mes_value;
}
}
Process.php 代碼
?php
//第一講話告訴瀏覽器返回的數(shù)據(jù)是xml格式
header("Content-Type:text/xml;charset=utf-8");
//告訴瀏覽器不要緩存數(shù)據(jù)
header("Cache-Control:no-cache");
//接收數(shù)據(jù)(這里要和請(qǐng)求方式對(duì)于 _POST 還是 _GET)
$username=$_POST['username'];
//這里我們看看如何處理格式是xml
$info="";
if($username=="李四"){
$info.="res>mes>用戶名不可以用,對(duì)不起/mes>/res>";//注意,這里數(shù)據(jù)是返回給請(qǐng)求的頁(yè)面.
}else{
$info.="res>mes>用戶名可以用,恭喜/mes>/res>";
}
echo $info;
?>
二、ajax如何處理json數(shù)據(jù)格式
json格式介紹
① json的格式如下 :
"{屬性名:屬性值,屬性名:屬性值,.... }"
因?yàn)閖son數(shù)據(jù)是原生態(tài)數(shù)據(jù),因此這種數(shù)據(jù)格式很穩(wěn)定,而且描述能力強(qiáng),我們建議大家使用json格式
② json數(shù)據(jù)格式的擴(kuò)展
如果服務(wù)器返回的json 是多組數(shù)據(jù),則格式應(yīng)當(dāng)如下:
$info="[{"屬性名":"屬性值",...},{"屬性名":"屬性值",...},....]";
在xmlhttprequest對(duì)象接收到j(luò)son數(shù)據(jù)后,應(yīng)當(dāng)這樣處理
//轉(zhuǎn)成對(duì)象數(shù)組
varreses=eval("("+xmlHttpRequest.responseText+")");
//通過(guò)reses可以取得你希望的任何一個(gè)值
reses[?].屬性名
③ 更加復(fù)雜的json數(shù)據(jù)格式
script language="JavaScript">
var people ={
"programmers":
[
{"firstName":"Brett", "email": "brett@newInstance.com" },
{"firstName":"Jason", "email": "jason@servlets.com" }
],
"writer":
[
{"writer":"宋江","age":"50"},
{"writer":"吳用","age":"30"}
],
"sex":"男"
};
window.alert(people.programmers[0].firstName);
window.alert(people.programmers[1].email);
window.alert(people.writer[1].writer);
window.alert(people.sex);
/script>
register.php 部分中chuli函數(shù)
function chuli(){
if(myXmlHttpRequest.readyState==4){
//------------看看如何取出json數(shù)據(jù)--------
var mes= myXmlHttpRequest.responseText;
//使用evla函數(shù)將mes轉(zhuǎn)換成相應(yīng)的對(duì)象
var mes_obj=eval("("+mes+")");
$("myres").value=mes_obj.res;
}
}
process.php 代碼
?php
header("Content-Type: text/html;charset=utf-8");
//告訴瀏覽器不要緩存數(shù)據(jù)
header("Cache-Control: no-cache");
$info="";
if($username=="1"){
$info='{"res":"該用戶不可用"}';
}
else{
//$info是一個(gè)json數(shù)據(jù)格式的字串
$info='{"res":"恭喜,用戶名可用"}';
}
echo $info;
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- JS實(shí)現(xiàn)圖片輪播效果實(shí)例詳解【可自動(dòng)和手動(dòng)】
- mongodb初始化并使用node.js實(shí)現(xiàn)mongodb操作封裝方法
- Nodejs核心模塊之net和http的使用詳解
- PHP中quotemeta()函數(shù)的用法講解
- 使用docker在服務(wù)器運(yùn)行多個(gè)php版本
- PHP封裝XML和JSON格式數(shù)據(jù)接口操作示例
- JS+php后臺(tái)實(shí)現(xiàn)文件上傳功能詳解
- PHPStorm中如何對(duì)nodejs項(xiàng)目進(jìn)行單元測(cè)試詳解
- 在PHP中輸出JS語(yǔ)句以及亂碼問(wèn)題的解決方案
- 實(shí)例說(shuō)明js腳本語(yǔ)言和php腳本語(yǔ)言的區(qū)別