只是寻找一些帮助,找出导致我的代码无法执行的原因。我已经创建了一个数据库和php来将它的数据传递给json格式。问题是我想把它传递到一个表中,但没有任何反应。 我的PHP代码
<?php
//$result = $conn->query("SELECT * FROM exampletable");
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "assignment6";
// Create connection
$conn = new mysqli('localhost', 'root', $password, $dbname) or die("Unable to connect");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM exampletable";
$result = mysqli_query($conn, $sql);
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
$conn->close();
?>
脚本
<script>
var obj, dbParam, xmlhttp, myObj, x, txt = "";
obj = { "Name":"customers" };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
txt += "<table><tr><th>Name</th></tr>"
for (x in myObj) {
txt += "<tr><td>" + myObj[x].Name + "</td></tr>";
}
txt += "</table>"
document.getElementById("demo").innerHTML = txt;
}
};
//https://www.w3schools.com/js/tryit.asp?filename=tryjson_php_db
xmlhttp.open("POST", "customer.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-
urlencoded");
xmlhttp.send("x=" + dbParam);
}
</script>
<p id="demo"></p>