js原生的ajax

function ajaxDom(){
  // 创建ajax对象
  var xhr = new XMLHttpRequest();
 
   //监听ajax的状态 到哪一步
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){
           alert(xhr.responseText);    //数据处理
    }
   }
 
  //发送请求
  xhr.open('get','index.php?id=1');       //用get传值的方式
  xhr.open('post','index.php');    //用post传值的方式
 
  //发送数据  get写null    post写键值对
  xhr.send(null);
  
  xhr.setRequestHeader('content-type','application/x-www-form-urlencode');
  var data = "id=1&type=add";
  xhr.send(data);
 }

posted @ 2018-12-29 13:37  跑很快的土豆  阅读(151)  评论(0编辑  收藏  举报