如有欠妥之处,请给予示正

先来前真个

<<!DOCTYPE html><html lang=&#34;en"><head> <meta charset="UTF-8"> <title>前后端分离</title></head><body><input type="button" value="获取数据" onclick="getdata()"> <script type="text/javascript"> function getdata(){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState==4){ eval("backdata="+xhr.responseText); xuanran(backdata.json); console.log(backdata); } }; xhr.open(&#39;get','./text.php'); xhr.send(null); } function xuanran(data){ for(x in data){ for(y in data[x]){ document.write(data[x][y]); } } } </script></body></html>

后真个

php前后端分离phpjsonmysql实现前后端分别 AJAX

<?phpheader('content-type:text/json');selectXiBu();function selectXiBu(){ $connection = mysqli_connect("localhost","root","","php"); if(!$connection){ echo "数据库连接缺点"; } else{ mysqli_query($connection,'set names utf8'); $selectSql = "select from student limit 50"; $result = mysqli_query($connection,$selectSql); $count=0; $aaaa=""; $json=""; while ($row = mysqli_fetch_array($result)){ $count++; $xh = $row['student'].$row['学号']; $xm = $row['student'].$row['姓名']; $sex = $row['student'].$row['性别']; $nl = $row['student'].$row['年事']; $dh = $row['student'].$row['电话']; $xb = $row['student'].$row['系部']; $zy = $row['student'].$row['专业']; $dz = $row['student'].$row['地址']; $key="s".$count; $value="\"学号\":\"$xh\",\"姓名\":\"$xm\",\"性别\":\"$sex\",\"年事\":\"$nl\",\"电话\":\"$dh\",\"系部\":\"$xb\",\"专业\":\"$zy\",\"地址\":\"$dz\""; $aaaa.="\"".$key."\"".":"."{".$value."}".($count<50?",":""); $json ="{\""."json"."\"".":{".$aaaa."}}"; } echo $json; }}?>