将ajax进行封装,不用导入外部jQuery文件
<script>
let $ = {
get(url, success) {
this.ajax({
url,
success
})
},
ajax({ type, url, async, success }) {
let ajax = new XMLHttpRequest()
ajax.open(type || 'GET', url, async || true)
ajax.send()
ajax.onreadystatechange = () => {
if (ajax.readyState === 4) {
if (ajax.status === 200) {
success(JSON.parse(ajax.response))
}
}
}
}
}
$.get("./a.json", function (data) {
console.log('打印', data);
})
</script>
a.json文件
{
"name": "attention",
"age": 20
}