Interaction1

var xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET","connectDatabase.php",true);
		xmlhttp.send();
	xmlhttp.onreadystatechange= function(){
		if(xmlhttp.readyState ==4 && xmlhttp.status ==200){
			str = xmlhttp.responseText;
			// console.log(typeof str);
			console.log(str);
			json_str = JSON.parse(str); //将返回json字符串转为js对象
			console.log(json_str);
<?php
 		$servername  = 'localhost';
 		$username = 'root';
 		$password = '';
 		$db = 'mydb';
 		// $name1 = $_GET["fname"];
 		// echo $name1;
 		$con = mysqli_connect($servername,$username,$password,$db);
 		if($con->connect_error){
 			die("error".$con->connect_error);
 		}
 		// echo "connect ok!";


 		$result = mysqli_query($con,"select firstname, lastname,email from myguests");
 		     $arr=[];
			while($row= mysqli_fetch_array($result,MYSQLI_ASSOC)){
 			// print_r($row);
 			// $arr[] = $row;   
				array_push($arr,$row);
			}
			// print_r($arr);
			// echo "-------------------";
 			echo json_encode($arr); //将数组对象转为json字符串
 		$con->close();
 		?>
原文地址:https://www.cnblogs.com/cyany/p/8465777.html