ajax數據返回進行遍歷的實例講解
更新時間:2018年08月08日 14:52:21 作者:OliverWeb
今天小編就為大家分享一篇ajax數據返回進行遍歷的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
后臺返回的數據:
{"receiveList":[{"receive_dept_id":"1007873","receive_dept_desc":"區(qū)公司領導","guid":"2016112316042622494230","receive_platform_id":"001"},{"receive_dept_id":"1007876","receive_dept_desc":"主任","guid":"2016112316042626240391","receive_platform_id":"001"}]}
其實仔細分析返回的格式,很簡單,就是一個Map,里面放了一個List,List里面有各種參數。
前端頁面的請求
$.ajax({
url:"/moduleAuthen/default.do?method=loadAllReceive",
dataType:"json", //返回的數據是json 格式
data:$("#fom1").serialize, //提交id為form1的所有參數
success:function(data){
var json =data.receiveList;
//第一種方式的遍歷
for(var index in json){
//其實index 就是個索引
var guid =json[index].guid;
var receive_dept_desc =json[index].receive_dept_desc;
}
//還有一種jquery 方式的遍歷,效果其實是一樣的,拿到后臺返回我們的數據,我們就可以進行各種操作了。
$.each(json,index){
var guid =json[index].guid;
var receive_dept_desc =json[index].receive_dept_desc;
}
}
})
{ "compress":[
<#if d_live??>
<#-- 參數:int pageNo -->
<#assign page = request.getParameter("page")!"null">
<#assign page = request.getParameter("page")!"null">
<#if page!="null" && page!="">
<#assign page = page?number>
<#else>
<#assign page=1>
</#if>
<#-- 參數:int pageSize-->
<#assign pagesize = request.getParameter("pagesize")!"null">
<#if pagesize!="null" && pagesize!="">
<#assign pagesize = pagesize?number>
<#else>
<#assign pagesize=10>
</#if>
<#if d_live.getLiveInfoList( page,pagesize,"3")??>
<#assign pagedContent = d_live.getLiveInfoList( page,pagesize,"3")!"null">
<#if pagedContent.getContent()??>
<#assign LiveInfo = pagedContent.getContent()!"null"> <#-- LiveInfo數據源 -->
<#if LiveInfo??>
<#list LiveInfo as liveInfo>
<#assign liveId = liveInfo.getLiveId()!"null">
<#assign liveInfoUrl = d_live.getLiveInfo("${liveId}")!"null">
<#assign liveUrl = liveInfoUrl.getPlayUrl()!"null"> <#-- 直播鏈接地址 -->
{"liveId":"${liveId}","liveUrl":"${liveUrl}"}
<#if liveInfo_has_next>
,
</#if>
</#list>
</#if>
</#if>
</#if>
</#if>
]
}
/*異步數據*/
var liveId= $("#liveId").val();
var asyurl="http://wap.cmread.com/rbc/t/liveindex.jsp?vt=9"+"&ol=560395";
alert("異步請求之前");
setTimeout(function(){
alert("進入定時器,還沒有異步");
$.ajax({
url:asyurl,
type:"POST",
dataType:"json",
success: function(data){
alert("異步獲取數據成功");
//var data = eval('('+data+')');
compress = data.compress;
for(var i=0;i<compress.length;i++){
alert("進入for循環(huán)");
var asyliveId=compress[i].liveId;
if(asyliveId==liveId){
alert("匹配相同");
var liveUrl=compress[i].liveUrl;
$("#live").find("source").attr("src",liveUrl);
}
}
alert("執(zhí)行第一次支部請求,改變地址是"+$("#live").find("source").attr("src"));
},error:function(){
console.log("error");
}
});
},3000);
以上這篇ajax數據返回進行遍歷的實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
[ASP.NET AJAX]Function對象及Type類的方法介紹
[ASP.NET AJAX]Function對象及Type類的方法介紹...2007-01-01
菜鳥蔡之Ajax復習第二篇(JQuery中的load()方法實現Ajax功能)
在上一篇博客中寫到的是在傳統(tǒng)的Javascript中使用XMLHttpRequest對象異步加載數據的,唉,童鞋.......看到那些代碼是不是有點頭疼啊!呵呵......2012-11-11

