json文件数据
[ {"name":"哈哈··","email":"邮箱01","gender":"男","hobby":["上网","打球"]}, {"name":"呵呵··","email":"邮箱02","gender":"男","hobby":["网购","打球"]} ]
读取json文件并显示到元素中
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="../lib/jquery/jquery.min.js"></script> </head> <body> <div id="reslut"></div> <script> $(document).ready(function(){ $.ajax({ /*请求方式*/ type:"GET", /*json文件位置*/ url:"user.js", /*返回数据格式为json*/ dataType:"json", /*请求成功完成后要执行的方法*/ success: function (data) { /*使用$.each方法遍历返回的数据data,*/ $.each(data, function (index,obj) { var content = obj.name +","+obj.email+","+obj.gender+","+obj.hobby[0]+","+obj.hobby[1]+"<br/>"; /*插入到类名为reslut的元素中*/ $("#reslut").append(content); }); } }) }); </script> </body> </html>
时间: 2024-10-15 17:54:56