[英]How to access json response object of data.submit() in JavaScript?
[英]access the response Json data in javascript
我在我的 HTML 中使用 Json 從數據庫中獲取數據,但我無法訪問返回的 Json 數據,這里是我的 HTML 函數:
$.ajax({
type: "POST",
url: 'fetch.php',
dataType: 'json',
data: {func:"viewCert",tracking_code:tracking_code},
success: function (data) {
if (data!=null) {
document.getElementById("generation_date").textContent = data.certInfo.timestamp;
} else {
alert("Something's Wrong! Try Later");
window.location = "../views/failed.html";
}
}
});
這是 fetch.php 函數:
function viewCert($tracking_code) {
$connect = connection();
$connect->set_charset("utf8");
$sql = "SELECT timestamp FROM certificate WHERE tracking_code=?";
$result = $connect->prepare($sql);
$result->bind_param("s",$tracking_code);
$result->execute();
$res=$result->get_result();
while ($row=$res->fetch_object()) {
$output[]=$row;
}
echo json_encode(array('certInfo' => $output));
}
抱歉這個問題我只是 HTML 和 Javascript 的新手,所以有人知道為什么時間戳不會設置在 'generation_date' 元素中嗎? 任何幫助都感激不盡
在您的 PHP 中, $output
似乎是一個數組。 因此,在您的 javascript 中,您需要訪問良好的索引以獲取數據。
嘗試 :
document.getElementById("generation_date").textContent = data.certInfo[0].timestamp;
----------------------------------------------------------------------^^^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.