【发布时间】:2021-11-12 18:54:28
【问题描述】:
我正在使用 json_encode 输出以发送到 javascript,使用此代码。
<?php
include "Connection.php";
$syntax_query = "select * from product";
$thisExecuter = mysqli_query($conn, $syntax_query);
$result = array();
while($row = mysqli_fetch_assoc($thisExecuter)){
array_push(
$result,
array(
"id" => $row["product_id"],
"name" => $row["product_name"]
)
);
}
echo json_encode($result);
?>
所以输出显示如下,
[{"id":"121353568","name":"Baju Casual - 黑色"},{"id":"556903232","name":"Tas LV - 红色"},{"id" :"795953280","name":"剑-木"},{"id":"834032960","name":"滑板车-铁板"}]
像这样编写javascript代码
function showHint() {
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
var obj = this.responseText;
document.getElementById("txtHint").innerHTML = obj.id;
}
xmlhttp.open("GET", "Download.php");
xmlhttp.send();
}
所以 obj.id 不起作用,输出显示未定义。
【问题讨论】:
标签: javascript php html json xmlhttprequest