原生js仿jquery的ajax()方法

时间:2020-9-3 作者:WilliamChan

原生js仿jquery的ajax()方法:



function ajax(){
  var ajaxData = {
    type:arguments[0].type || "GET",
    url:arguments[0].url || "",
    async:arguments[0].async || "true",
    data:arguments[0].data || null,
    dataType:arguments[0].dataType || "text",
    contentType:arguments[0].contentType || "application/x-www-form-urlencoded",
    beforeSend:arguments[0].beforeSend || function(){},
    success:arguments[0].success || function(){},
    error:arguments[0].error || function(){}
  }
  ajaxData.beforeSend()
  var xhr = createxmlHttpRequest(); 
  xhr.responseType=ajaxData.dataType;
  xhr.open(ajaxData.type,ajaxData.url,ajaxData.async); 
  xhr.setRequestHeader("Content-Type",ajaxData.contentType); 
  xhr.send(convertData(ajaxData.data)); 
  xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4) { 
      if(xhr.status == 200){
        ajaxData.success(xhr.response)
      }else{
        ajaxData.error()
      } 
    }
  } 
}
  
function createxmlHttpRequest() { 
  if (window.ActiveXObject) { 
    return new ActiveXObject("Microsoft.XMLHTTP"); 
  } else if (window.XMLHttpRequest) { 
    return new XMLHttpRequest(); 
  } 
}
  
function convertData(data){
  if( typeof data === 'object' ){
    var convertResult = "" ; 
    for(var c in data){ 
      convertResult+= c + "=" + decodeURIComponent(data[c]) + "&"; 
    } 
    convertResult=convertResult.substring(0,convertResult.length-1)
    return convertResult;
  }else{
    return data;
  }
}

用法:


ajax({
  type:"POST",
  url:"ajax.php",
  dataType:"json",
  data:{"val1":"abc","val2":123,"val3":"456"},
  beforeSend:function(){
    //some js code
  },
  success:function(msg){
    console.log(msg)
  },
  error:function(){
    console.log("error")
  }
})

以上就是知道不整理的《原生js仿jquery的ajax()方法》,希望对您有所帮助。

声明:若您对本文内容有任何意见,欢迎发送邮件至:admin@zhidaobu.com,本站会尽快回应并做出相应处理。