欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery ajax json 數(shù)據(jù)的遍歷代碼

 更新時(shí)間:2016年06月12日 17:14:49   作者:編程足球  
最近做了一個(gè)項(xiàng)目,其中有需求要進(jìn)行ajax請求后,后臺傳遞回來以下json數(shù)據(jù)。下面小編給大家分享我的實(shí)現(xiàn)思路,需要的朋友參考下

先給大家說下我的需求:進(jìn)行ajax請求后,后臺傳遞回來以下json數(shù)據(jù)。

具體實(shí)現(xiàn)代碼如下所示:

JavaScript代碼

{ 
"data":[ 
{"id":"1","name":"選擇A","value":"A"}, 
{"id":"2","name":"選擇B","value":"B"}, 
{"id":"3","name":"選擇C","value":"C"} 
] 
} 

對上面的json數(shù)據(jù)類在jquery 的success 函數(shù)中解析

JavaScript代碼

$.ajax({ 
type: "POST", 
url: "xxx.do", 
dataType : "json", // 指定返回類型 
data: {xxx:"yyy"}, // 傳遞到后臺的參數(shù) 
success: function(data) 
{ 
$.each(data, function(index,values){ // 解析出data對應(yīng)的Object數(shù)組 
$.each(values,function(index2,value){ // 遍歷Object數(shù)組 ,每個(gè)對象的值存放在value ,index2表示為第幾個(gè)對象 
// 根據(jù)自己的邏輯進(jìn)行數(shù)據(jù)的處理 
alert(value.name + " " + value.value); 
// TODO: 邏輯 
}); 
}); 
}, 
error : function() 
{ 
alert("系統(tǒng)出現(xiàn)問題"); 
} 
}); 

繼續(xù)來個(gè)例子

本章節(jié)分享一段代碼實(shí)例,是jQuery封裝的ajax對JSON數(shù)據(jù)的請求處理。
代碼比較簡單,對于有經(jīng)驗(yàn)的人員來說可以掠過,初學(xué)者可以做一下參考之用。
代碼如下:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<title>腳本之家</title> 
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$(document).ready(function(){
 $("#bt").click(function(){
  $.ajax({
   type: "get",
   dataType: "json",
   url: "demo/jQuery/ajax/txt/json.txt",
   success: function(msg){
    var data = msg
    str="";
    $.each(data,function(index, n){
     str=str+data[index].webName+","+data[index].url+","+data[index].age+"<br/>";
    });
    $("#show").html(str);
   }
  });
 })
})
</script>
</head>
<body>
<div id="show"></div>
<input type="button" id="bt" value="查看效果"/>
</body>
</html>

以上所述是小編給大家介紹的jQuery ajax json 數(shù)據(jù)的遍歷代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論