登陆

怎么实现从接口获取的信息的表格局部自动刷新

html如下:


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
<!-- <meta http-equiv="refresh" content="2"> -->
<script language="javascript" src="../JS/login.js"></script>
<link rel="stylesheet" href="../CSS/login.css" type="text/css">
</head>
<body onload="Login();">
 <div class="main">
 
 
 
 
 
  <div id="myDiv" class="table1box">
  
  
   <table id="table1" class="table1" border="1px">
    <tr>
     <th>信息</th>
     <th>msg</th>
     <th>认证码</th>
    </tr>
    </div>
    </div>
   </table>
   
   
</body>
</html>



js如下:

function Login() {
 var xmlhttp;
 if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
 } else {
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 }

 xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   var datastr = xmlhttp.responseText;
   var dataobj = JSON.parse(datastr)
   let dataarr = [];
   for (let i in dataobj) {
    dataarr.push(dataobj[i]);
   }

   var table = document.getElementById("table1");
   var row = table.insertRow(table.rows.length);
   var c1 = row.insertCell(0);
   c1.innerHTML = dataarr[0];
   var c2 = row.insertCell(1);
   c2.innerHTML = dataarr[1];
   var c3 = row.insertCell(2);
   c3.innerHTML = dataarr[2];
  }
 }
 xmlhttp.open("POST", "https://api.123321yun.com/api/user/login?username=test123&password=123456", true);
 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 xmlhttp.send();
 
}

小白求帮忙

# HTML # JavaScript # AJAX
胡行东胡行东143 天前245 次浏览

全部回复(2)我要回复

  • 秋香姐家的小书童

    秋香姐家的小书童2019-03-31 11:44:50

    1。确定要刷新 的元素 比如  id为 #cont

    2。ajax不停的请求服务器(后端的逻辑自己写,什么条件下需要更新内容)

    3。当请求到新的内容 就将元素#cont内容进行修改

    就这么思路

    回复
    0
  • 取消回复发送