- 实例化XMLHttpRequest对象
- 连接服务器
- 发送请求
function ajax(options){
let method = options.menthod || 'GET',
data = options.data,
params = options.params,
url = options.url + (params? '?'+Object.keys(params).map(key=>key+'='+params[key]).join('&'):''),
async = options.async === false?false:true,
sucess = options.success,
headers = options.headers;
let xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
xhr.onreadystatechange = function(){
if(xhr.readyState===4&&xhr.status===200){
success&&success(xhr.responseText);
}
}
xhr.open(mength,url,async);
if(headers){
Objext.key(Headers).forEach(key=>xhr.setRequestHeader(key,headers[key]));
}
method === 'GET'?xhr.send():xhr.send(data);
}