<!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>
</head>
<body>
<button type="button" name="button" class="btn">請求數據</button>
<script type="text/javascript">
window.onload = function() {
var btn = document.querySelector('.btn')
btn.addEventListener('click',function() {
$ajax({
methos: 'get',
url: '1.txt'
})
},false)
function $ajax(options){
options.method = options.method || 'get';
options.url = options.url || '';
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if( (xhr.readyState === 4) && (xhr.status>=200&&xhr.status<300 || xhr.status === 304) ){
console.log('請求成功!');
}
}
xhr.open(options.method, options.url, true);
xhr.send(null);
}
}
</script>
</body>
</html>