ajax数据返回进行遍历的实例讲解
后台返回的数据:
{"receiveList":[{"receive_dept_id":"1007873","receive_dept_desc":"区公司领导","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循环"); var asyliveId=compress[i].liveId; if(asyliveId==liveId){ alert("匹配相同"); var liveUrl=compress[i].liveUrl; $("#live").find("source").attr("src",liveUrl); } } alert("执行第一次支部请求,改变地址是"+$("#live").find("source").attr("src")); },error:function(){ console.log("error"); } }); },3000);
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05