jQuery ajax讀取json文件的示例:json文件:1234[{"name":"哈哈··","email":"郵箱01","gender":"男","hobby":["上網","打球"]},{"name":"呵呵··","email":"郵箱02","gender":"男","hobby":["網購","打球"]}]js代碼:12345678910111213141516171819202122<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", //返回數(shù)據(jù)格式為json dataType: "json", //請求成功完成后要執(zhí)行的方法 success: function(data){ //使用$.each方法遍歷返回的數(shù)據(jù)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>
- 1 回答
- 0 關注
- 772 瀏覽
添加回答
舉報
0/150
提交
取消