<!DOCTYPE HTML><HTMLLang= "zh"> <Head> <MetaCharSet= "UTF-8" /> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0" /> <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge" /> <Scriptsrc= "Js/jquery-3.3.1.js"type= "Text/javascript"CharSet= "Utf-8"></Script> <title>Document</title> </Head> <Bodystyle= "padding-top:30px;"> <ButtonID= "Btnjson">Json</Button> <Tableclass= "Table"></Table> <Scripttype= "Text/javascript"> $(function() { $("#btnJson"). Click (function() {$.ajax ({type:"Get", URL:"json.php", DataType:"JSON", Async:true, Success:function(data) {varitem; $.each (data,function(i, result) {Item= "<tr><td>" +result[' City'] + "</td><td>" +result['Phone'] + "</td></tr>"; $('. Table'). Append (item); }); } }); }) }) </Script> </Body></HTML>
Json. Php
<?PHP$host= ' 127.0.0.1 '; $user= ' Root '; $password= ' '; $database= ' Yiibaidb '; $conn=NewMysqli ($host,$user,$password,$database); if(!$conn) { die("Database connection failed! " .$conn-connect_error); } $sql= "SELECT * from ' Offices '"; $result=$conn, Query ($sql); $arr=Array(); if($resultNum_rows > 0) { while($rows=$result-Fetch_assoc ()) { $arr[] =$rows; } } EchoJson_encode ($arr); $conn-close ();?>
Ajax takes PHP JSON data and displays