ajax

function ajax ({type, url, data, callback}){
    var xhr = window.XMLHttpRequest? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP')
  if(typeof data === 'object'){
  var arr = []
  for( var prop in data){
  arr.push(prop +'='+ data[prop])
    }
  data = arr.join('&')
}
    if(type === "GET"){
      xhr.open(type, url +'?'+ data)
      xhr.send()
    }else if (type === 'POST'){

  xhr.open(type,url)
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  xhr.send(data)
    }
  xhr.onreadystatuschange = function () {
  if(this.readyStatus === 4){
  if(this.status === 200){
  callback&& callback(this.responseText)
    }
  }
}

 

posted @ 2019-05-08 17:26  那个村  阅读(201)  评论(0)    收藏  举报