欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php+Ajax處理xml與json格式數(shù)據(jù)的方法示例

 更新時間:2019年03月04日 09:07:51   作者:致Great  
這篇文章主要介紹了php+Ajax處理xml與json格式數(shù)據(jù)的方法,結(jié)合實例形式分析了ajax處理xml格式數(shù)據(jù)及json格式數(shù)據(jù)相關(guān)操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了php+Ajax處理xml與json格式數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

一、ajax如何處理xml數(shù)據(jù)格式

register.php

只需修改上一篇《php+Ajax無刷新驗證用戶名操作》中chuli函數(shù)部分

functionchuli(){
 // window.alert("cuhli函數(shù)被調(diào)用"+myXmlHttpRequest.readyState);
  //我要取出從register.php返回的數(shù)據(jù)
  if(myXmlHttpRequest.readyState==4){
    //------------看看如何取出xml數(shù)據(jù)--------
    //獲取mes節(jié)點
    var mes=myXmlHttpRequest.responseXML.getElementsByTagName("mes");
    //取出mes節(jié)點值
    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ù)(這里要和請求方式對于 _POST 還是 _GET)
    $username=$_POST['username'];
    //這里我們看看如何處理格式是xml
    $info="";
    if($username=="李四"){
       $info.="<res><mes>用戶名不可以用,對不起</mes></res>";//注意,這里數(shù)據(jù)是返回給請求的頁面.
    }else{
       $info.="<res><mes>用戶名可以用,恭喜</mes></res>";
    }
    echo $info;
?>

二、ajax如何處理json數(shù)據(jù)格式

json格式介紹

① json的格式如下 :

"{屬性名:屬性值,屬性名:屬性值,.... }"

因為json數(shù)據(jù)是原生態(tài)數(shù)據(jù),因此這種數(shù)據(jù)格式很穩(wěn)定,而且描述能力強,我們建議大家使用json格式

② json數(shù)據(jù)格式的擴展

如果服務(wù)器返回的json 是多組數(shù)據(jù),則格式應(yīng)當(dāng)如下:

$info="[{"屬性名":"屬性值",...},{"屬性名":"屬性值",...},....]";

在xmlhttprequest對象接收到j(luò)son數(shù)據(jù)后,應(yīng)當(dāng)這樣處理

//轉(zhuǎn)成對象數(shù)組
varreses=eval("("+xmlHttpRequest.responseText+")");
//通過reses可以取得你希望的任何一個值
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)的對象
    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是一個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基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論