- 首先配置好Apache服务器
- HTML文件要在 文件目录下
- 编写HTML文件`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="jquery-3.4.1.js"></script>
<style> </style>
</head>
<body>
<div> </div>
<button id="btn">btn</button>
<script>
$(function(){
window.onload = function(){
var btn = document.getElementById('btn')
btn.onclick = function(){
$.ajax({
type:"GET",
url:"data.json",
data:"",
success:function(msg){
console.log(msg);
console.log(msg[0].name);
console.log(msg[1]);
console.log(msg[2]);
console.log(msg[3]);
},
error:function(msg){
alert(xhr.status)
}
})
}
}
})
</script>
</body>
</html>
data.json 文件
[
{
"name":"lisa",
"age":18,
"habit":"dance"
},
{
"name":"jennie",
"age":20,
"habit":"sing"
},
{
"name":"rose",
"age":19,
"habit":"sing+1"
},
{
"name":"jisoo",
"age":18,
"habit":"smile"
}
]
- 访问 abcajax.com获取数据
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191104161843296.png)