封装一个XMLHttpRequest
function ajax(method,url,callback,data){
let xhr = new XMLHttpRequest() || new ActiveXObject(‘Microsoft.XMLHTTP‘)
xhr.open(method,url,true)
if(method === ‘get‘){
xhr.send()
}
if(method===‘post‘){
xhr.setRequestHeader(‘Content-Type‘,‘application/json‘)
xhr.send(JSON.stringify(data))
}
xhr.onreadystatechange = function(){
if(this.readyState === 4 && this.status === 200){
callback(JSON.parse(this.responseText))
}
}
}
相关推荐
jiaguoquan00 2020-07-07
zhaolisha 2020-06-12
坚持着执着 2020-06-05
knightwatch 2020-04-25
zhaolisha 2020-01-11
zhaolisha 2019-12-29
wanghongsha 2019-12-06
小小大人物 2014-01-19
loopstang 2014-01-11
roodyszz0 2019-11-17
81493369 2019-11-04
Liuzhiqianblog 2019-11-03
89241846 2019-09-12
青菜萝卜白菜 2019-07-03
荒唌Blog 2017-12-18
无材可去补苍天 2019-06-12
HAcSeeking 2018-11-05
81530694 2013-03-14