1、写一个 json 文件 data.json
{
{
"id": 1,
"username": "tiantian",
"age": 23,
},
{
"id": 2,
"username": "lzp",
"age": 28,
}
}
2、 封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
function get(url, callback) {
var oReq = new XMLHttpRequest()
// 当请求加载成功之后要调用指定的函数
oReq.onload = function () {
// 我现在需要得到这里的 oReq.responseText
callback(oReq.responseText)
}
oReq.open("get", url, true)
oReq.send()
}
get('data.json', function (data) {
console.log(data)
})
</script>
</body>
</html>