ajax

310 阅读1分钟
<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 ({name,age}) { console.log('打印', name,age);    })    
</script>