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ā)送請求到服務(wù)器的某個頁面
//第一個參數(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("服務(wù)器返回"+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)換為對應(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="驗證用戶名">
<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ù)格式的擴展
如果服務(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. 如果你的項目經(jīng)理沒有特殊的要求,建議使用json
b. 若應(yīng)用程序不需要與其他應(yīng)用程序共享數(shù)據(jù)的時候, 使用 HTML 片段來返回數(shù)據(jù)時最簡單的
c. 如果數(shù)據(jù)需要重用, JSON 文件是個不錯的選擇, 其在性能和文件大小方面有優(yōu)勢
d. 當(dāng)遠程應(yīng)用程序未知時, XML 文檔是首選, 因為 XML 是 web 服務(wù)領(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)文章
Jquery中ajax提交表單幾種方法(get、post兩種方法)
ajax技術(shù)帶給我們的是良好的用戶體驗,同時,使用jquery可以簡化開發(fā),提高工作效率,接下來,腳本之家小編給大家分享Jquery中ajax提交表單幾種方法,需要的朋友可以參考下2015-09-09
關(guān)于Ajax技術(shù)中servlet末尾的輸出流
這篇文章主要介紹了關(guān)于Ajax技術(shù)中servlet末尾的輸出流的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
jQuery AJAX中readyState與status的區(qū)別與聯(lián)系
這篇文章主要介紹了 jQuery AJAX中readyState與status的區(qū)別與聯(lián)系,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
Ajax讀取數(shù)據(jù)到表格的實現(xiàn)代碼
前兩篇我們講了"ajax開始的準(zhǔn)備"與及如何使用ajax技術(shù)進行簡單的數(shù)據(jù)讀?。裉煳覀円v的是:使用Ajax無刷新技術(shù)讀取服務(wù)端多條數(shù)據(jù),并將返回的數(shù)據(jù)顯示到一個表格內(nèi).2010-08-08
使用Ajax技術(shù)通過XMLHttpRequest對象完成首頁登錄功能
這篇文章主要介紹了使用Ajax技術(shù)通過XMLHttpRequest對象完成首頁登錄功能,很不錯的嘗試,需要的朋友可以參考下2014-08-08

