首页 > 技术文章 > ajax获取服务器响应信息

wangshengl9263 2018-05-12 19:16 原文

window.onload = function(){
  document.getElementById('btn').onclick = function(){
    var req = new XMLHttpRequest();
    req.open('get', 'demo.php');   //建立服务器连接 get 或 post请求
    req.onreadystatechange = function(){   //ajax请求状态变化
      if(req.readyState == 4 && req.statu == 200){   //200状态表示返回成功
        var str = req.responseText;
        alert(str)
      }
    }
      req.send();//对服务器发送请求
   }
}

推荐阅读