jQuery ajax读取json文件的示例:
json文件:
1 2 3 4 | [ {"name":"哈哈··","email":"邮箱01","gender":"男","hobby":["上网","打球"]}, {"name":"呵呵··","email":"邮箱02","gender":"男","hobby":["网购","打球"]} ] |
js代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <script type="text/javascript" src="js/jquery.1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function() { $.ajax({ //请求方式为get type:"GET", //json文件位置 url:"user.json", //返回数据格式为json dataType: "json", //请求成功完成后要执行的方法 success: function(data){ //使用$.each方法遍历返回的数据date,插入到id为#result中 $.each(data,function(i,item){ var content=item.name+","+item.email+","+item.gender+ ","+item.hobby[0]+","+item.hobby[1]+"</br>"; $("#result").append(content); }) } }) }); </script> |
相关分类