get获取Json

时间:2023-03-08 17:04:09

前端代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<title></title>
</head>
<body>
<div >
小丁的年龄是:<span id="dsh"></span>
</div>
<script type="text/javascript">
$.ajax({
type: "get",
url: "DB.json",
datatype: "json",//"xml", "html", "script", "html", "jsonp", "text".
success: function (data,status) {
document.getElementById("dsh").innerHTML=data.teacher[1].age;
//var js=JSON.stringify(data);//转换成json字符串
},
error: function (data,status) { alert("Ajax请求Json错误");}
});
</script>
</body>
</html>

DB.json

{ "student": [
{ "name": "王老师", "age": "40" },
{ "name": "丁老师", "age": "30" }
],
"teacher": [
{ "name": "小王", "age": "20" },
{ "name": "小丁", "age": "10" }
] }

效果

get获取Json