json文件
{
? ? "first":[
? ? ? ? {"name":"張三","sex":"男"},
? ? ? ? {"name":"李四","sex":"男"},
? ? ? ? {"name":"王武","sex":"男"},
? ? ? ? {"name":"李梅","sex":"女"}
? ? ]
}
方法一:
$.ajax({
? url: "ceshi.json",//json文件位置
? type: "GET",//請求方式為get
? dataType: "json", //返回數據格式為json
? success: function(data) {//請求成功完成后要執行的方法
? ? ? //each循環 使用$.each方法遍歷返回的數據date
? ? ? $.each(data.first, function(i, item) {
? ? ? ? ? ? var str = '<div>姓名:' + item.name + '性別:' + item.sex + '</div>';
? ? ? ? ? ? document.write(str);
? ? ? })
? }
})
方法二:
// jQuery.getJSON( url [, data ] [, success ] )
$.getJSON("ceshi.json", "", function(data) {
? ? ? //each循環 使用$.each方法遍歷返回的數據date
? ? ? $.each(data.first, function(i, item) {
? ? ? ? ? ? var str = '<div>姓名:' + item.name + '性別:' + item.sex + '</div>';
? ? ? ? ? ? document.write(str);
? ? ? })
});