Home > Web Front-end > JS Tutorial > How can ajax read local json

How can ajax read local json

php中世界最好的语言
Release: 2017-12-30 16:53:56
Original
2293 people have browsed it

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":"女"}
  ]
}
Copy after login


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 = &#39;<div>姓名:&#39; + item.name + &#39;性别:&#39; + item.sex + &#39;</div>&#39;;
      document.write(str);
    })
  }
})
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template