What I bring to you this time is how ajax can read local json. In the project, we use json almost frequently. This article will give you a good analysis.
json file
{ "first":[ {"name":"张三","sex":"男"}, {"name":"李四","sex":"男"}, {"name":"王武","sex":"男"}, {"name":"李梅","sex":"女"} ] }
js:
Method 1:
$.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); }) } })
I believe you have mastered the method after reading the above introduction. For more exciting information, please pay attention to the php Chinese websiteOther related articles!
Related reading:
How to delete the drag animation of the style
html generated after the link is clicked Implementation method
How to customize the console object during the use of JS
The above is the detailed content of How can ajax read local json. For more information, please follow other related articles on the PHP Chinese website!