How to implement partial automatic refresh of table information obtained from the interface
胡行东
胡行东 2019-03-29 11:02:54
0
2
1120

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();
 
}

小白求帮忙

胡行东
胡行东

reply all(1)
秋香姐家的小书童

1. Determine the element to be refreshed, for example, the id is #cont

2. Ajax keeps requesting the server (the back-end logic is written by oneself, under what conditions the content needs to be updated)

3. When new content is requested, the content of element #cont will be modified

That’s the idea

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template