本篇文章給大家分享的是有關(guān)使用ajax怎么處理返回的json數(shù)據(jù),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司,專注成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營銷推廣,空間域名,網(wǎng)絡(luò)空間,網(wǎng)站托管有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問題,請聯(lián)系創(chuàng)新互聯(lián)。
以用戶注冊為例:
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="";
//驗(yàn)證用戶名是否存在
function checkName(){
myXmlHttpRequest=getXmlHttpObject();
//怎么判斷創(chuàng)建ok
if(myXmlHttpRequest){
//通過myXmlHttpRequest對象發(fā)送請求到服務(wù)器的某個頁面
//第一個參數(shù)表示請求的方式, "get" / "post"
//第二個參數(shù)指定url,對哪個頁面發(fā)出ajax請求(本質(zhì)仍然是http請求)
//第三個參數(shù)表示 true表示使用異步機(jī)制,如果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í)際的數(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("服務(wù)器返回"+myXmlHttpRequest.responseText);
//$('myres').value=myXmlHttpRequest.responseText;
//看看如果取出 xml格式數(shù)據(jù)
//window.alert(myXmlHttpRequest.responseXML);
//取出text或json數(shù)據(jù)用下面方式:獲取mes節(jié)點(diǎn)
var mes=myXmlHttpRequest.responseText;
window.alert(mes);
//使用 eval 函數(shù)將 mes字符串轉(zhuǎn)換為對應(yīng)的對象,注意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="驗(yàn)證用戶名">
<input 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的格式如下 :
"{屬性名:屬性值,屬性名:屬性值,.... }"
因?yàn)閖son數(shù)據(jù)是原生態(tài)數(shù)據(jù),因此這種數(shù)據(jù)格式很穩(wěn)定,而且描述能力強(qiáng),我們建議大家使用json格式
2、 json數(shù)據(jù)格式的擴(kuò)展
如果服務(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[?].屬性名
3、 更加復(fù)雜的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、當(dāng)一個ajax請求到服務(wù)器,服務(wù)器可以根據(jù)需求返回 三種格式的數(shù)據(jù),那么我們應(yīng)當(dāng)選擇哪一個?
a. 如果你的項(xiàng)目經(jīng)理沒有特殊的要求,建議使用json
b. 若應(yīng)用程序不需要與其他應(yīng)用程序共享數(shù)據(jù)的時候, 使用 HTML 片段來返回?cái)?shù)據(jù)時最簡單的
c. 如果數(shù)據(jù)需要重用, JSON 文件是個不錯的選擇, 其在性能和文件大小方面有優(yōu)勢
d. 當(dāng)遠(yuǎn)程應(yīng)用程序未知時, XML 文檔是首選, 因?yàn)?XML 是 web 服務(wù)領(lǐng)域的 “世界語”
ajax是一種在無需重新加載整個網(wǎng)頁的情況下,能夠更新部分網(wǎng)頁的技術(shù),可以通過在后臺與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,使網(wǎng)頁實(shí)現(xiàn)異步更新。
以上就是使用ajax怎么處理返回的json數(shù)據(jù),小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:使用ajax怎么處理返回的json數(shù)據(jù)
轉(zhuǎn)載注明:http://chinadenli.net/article20/jgghjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、小程序開發(fā)、網(wǎng)站導(dǎo)航、全網(wǎng)營銷推廣、品牌網(wǎng)站設(shè)計(jì)、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)