ajax循环请求json数据库,ajax请求json用each和for循环遍历+储存session基本案例

json

{

"test":[

{"id":"1","dsn":"alibaba","pn":"payfor","src":"./img/1.jpg","email":"yang@qq.com"},

{"id":"2","dsn":"tengxun","pn":"QQ","src":"./img/2.jpg","email":"longdaye@qq.com"},

{"id":"3","dsn":"google","pn":"sq","src":"./img/3.jpg","email":"l23@qq.com"}

]

}

css

.ea{margin:0;padding:0}

.ea li {float:left;margin-right:1px;text-decoration:none;}

html

js

$(function() {

$.ajax({

type: "get",

url: "js/data.json",

async: true,

dataType: "json",

success: function(data) {

console.log(data);

//var msg = ''

//each循环

// $.each(data.test, function(i, v) {

// console.log(v.dsn);

// msg += "

+v.src+"

// });

//$('.ea').html(msg)

//for循环

var mss = '';

for(var i = 0; i < data.test.length; i++) {

console.log(data.test[i].src)

mss += "

+%20data.test%5Bi%5D.src%20+%20"

}

$(".ea").html(mss);

sessionStorage.setItem('ds', data.test[1].id);

var dd = sessionStorage.getItem('ds')

console.log(dd);

},

error: function(data) {

console.log(data)

}

});

})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值