ajax處理返回的json格式數(shù)據(jù)方法
以用戶注冊為例:
register.php
<html> <head> <title>用戶注冊</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <script type="text/javascript"> //創(chuàng)建ajax引擎 function getXmlHttpObject(){ var xmlHttpRequest; //不同的瀏覽器獲取對象xmlhttprequest 對象方法不一樣 if(window.ActiveXObject){ xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP"); }else{ xmlHttpRequest=new XMLHttpRequest(); } return xmlHttpRequest; } var myXmlHttpRequest=""; //驗證用戶名是否存在 function checkName(){ myXmlHttpRequest=getXmlHttpObject(); //怎么判斷創(chuàng)建ok if(myXmlHttpRequest){ //通過myXmlHttpRequest對象發(fā)送請求到服務器的某個頁面 //第一個參數(shù)表示請求的方式, "get" / "post" //第二個參數(shù)指定url,對哪個頁面發(fā)出ajax請求(本質(zhì)仍然是http請求) //第三個參數(shù)表示 true表示使用異步機制,如果false表示不使用異步 var url="regisgerProcess.php"; //這個是要發(fā)送的數(shù)據(jù) var data="username="+$('username').value; //打開請求. myXmlHttpRequest.open("post",url,true); //還有一句話,這句話必須. myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //指定回調(diào)函數(shù).chuli是函數(shù)名 myXmlHttpRequest.onreadystatechange=chuli; //真的發(fā)送請求,如果是get請求則填入 null即可 //如果是post請求,則填入實際的數(shù)據(jù) myXmlHttpRequest.send(data); } } //回調(diào)函數(shù) function chuli(){ //window.alert("處理函數(shù)被調(diào)回"+myXmlHttpRequest.readyState); //我要取出從registerPro.php頁面返回的數(shù)據(jù) if(myXmlHttpRequest.readyState==4){ //取出值,根據(jù)返回信息的格式定.text //window.alert("服務器返回"+myXmlHttpRequest.responseText); //$('myres').value=myXmlHttpRequest.responseText; //看看如果取出 xml格式數(shù)據(jù) //window.alert(myXmlHttpRequest.responseXML); //取出text或json數(shù)據(jù)用下面方式:獲取mes節(jié)點 var mes=myXmlHttpRequest.responseText; window.alert(mes); //使用 eval 函數(shù)將 mes字符串轉(zhuǎn)換為對應的對象,注意eval函數(shù)格式如下: mes_obj = eval ("(" + mes + ")"); window.alert(mes_obj.res); $('myres').value=mes_obj.res; } } //這里我們寫一個函數(shù) function $(id){ return document.getElementById(id); } </script> </head> <body> <form action="regisgerProcess.php" method="post"> 用戶名字:<input type="text" name="username1" id="username"><input type="button" onclick="checkName();" value="驗證用戶名"> <input style="border-width: 0;color: red" type="text" id="myres"> <br/> 用戶密碼:<input type="password" name="password"><br> 電子郵件:<input type="text" name="email"><br/> <input type="submit" value="用戶注冊"> </form> <form action="???" method="post"> 用戶名字:<input type="text" name="username2" > <br/> 用戶密碼:<input type="password" name="password"><br> 電子郵件:<input type="text" name="email"><br/> <input type="submit" value="用戶注冊"> </form> </body> </html>
regisgerProcess.php:
<?php //這里兩句話很重要,第一講話告訴瀏覽器返回的數(shù)據(jù)格式,若返回xml格式數(shù)據(jù),此處寫header("Content-Type: text/xmla;set=utf-8"); , //若返回tex或json數(shù)據(jù),此處填寫header("Content-Type: text/html;charset=utf-8"); header("Content-Type: text/html;charset=utf-8"); //告訴瀏覽器不要緩存數(shù)據(jù) header("Cache-Control: no-cache"); //接收數(shù)據(jù)(這里要和請求方式對于 _POST 還是 _GET) $username=$_POST['username']; //這里我們看看如何處理格式是json $info=""; if($username=="shunping"){ $info.='{"res":"用戶名可用"}';//注意,這里數(shù)據(jù)是返回給請求的頁面. }else{ $info.='{"res":"用戶名不可用","id":"001"}'; } echo $info; ?>
json數(shù)據(jù)詳解:
1、json的格式如下 :
"{屬性名:屬性值,屬性名:屬性值,.... }"
因為json數(shù)據(jù)是原生態(tài)數(shù)據(jù),因此這種數(shù)據(jù)格式很穩(wěn)定,而且描述能力強,我們建議大家使用json格式
2、 json數(shù)據(jù)格式的擴展
如果服務器返回的json 是多組數(shù)據(jù),則格式應當如下:
$info="[{"屬性名":"屬性值",...},{"屬性名":"屬性值",...},....]";
在xmlhttprequest對象接收到json數(shù)據(jù)后,應當這樣處理
//轉(zhuǎn)成對象數(shù)組
varreses=eval("("+xmlHttpRequest.responseText+")");
//通過reses可以取得你希望的任何一個值
reses[?].屬性名
3、 更加復雜的json數(shù)據(jù)格式
<scriptlanguage="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>
4、當一個ajax請求到服務器,服務器可以根據(jù)需求返回 三種格式的數(shù)據(jù),那么我們應當選擇哪一個?
a. 如果你的項目經(jīng)理沒有特殊的要求,建議使用json
b. 若應用程序不需要與其他應用程序共享數(shù)據(jù)的時候, 使用 HTML 片段來返回數(shù)據(jù)時最簡單的
c. 如果數(shù)據(jù)需要重用, JSON 文件是個不錯的選擇, 其在性能和文件大小方面有優(yōu)勢
d. 當遠程應用程序未知時, XML 文檔是首選, 因為 XML 是 web 服務領(lǐng)域的 “世界語”
以上這篇ajax處理返回的json格式數(shù)據(jù)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- JS如何操作DOM基于表格動態(tài)展示數(shù)據(jù)
- vue+vuex+json-seiver實現(xiàn)數(shù)據(jù)展示+分頁功能
- mockjs+vue頁面直接展示數(shù)據(jù)的方法
- Vue.js 實現(xiàn)數(shù)據(jù)展示全部和收起功能
- json數(shù)據(jù)傳到前臺并解析展示成列表的方法
- jQuery插件jsonview展示json數(shù)據(jù)
- Springmvc處理ajax請求并返回json數(shù)據(jù)
- 使用fastjson中的JSONPath處理json數(shù)據(jù)的方法
- 如何處理后臺向前臺傳遞的json數(shù)據(jù)
- 對pandas處理json數(shù)據(jù)的方法詳解
- ajax請求后臺接口數(shù)據(jù)與返回值處理js的實例講解
- js前端對于大量數(shù)據(jù)的展示方式及處理方法
相關(guān)文章
javascript請求servlet實現(xiàn)ajax示例(分享)
下面小編就為大家?guī)硪黄猨avascript請求servlet實現(xiàn)ajax示例(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06AJAX 請求區(qū)分 $_SERVER[''HTTP_X_REQUESTED_WITH''] 小解
關(guān)于這個內(nèi)容,很多人都有所了解.但從我搜索的內(nèi)容來看,他們只是略微看一下,根本不知道里面到底是什么情況.2011-06-06SSH網(wǎng)上商城之使用ajax完成用戶名是否存在異步校驗
這篇文章主要介紹了SSH網(wǎng)上商城之使用ajax完成用戶名是否存在異步校驗的相關(guān)資料,需要的朋友可以參考下2015-12-12