access the response Json data in javascript

问题: I'm using a Json in my HTML to get data from Database but I can't access the data of returned Json here is my HTML function : $.ajax({ type: "POST", url: 'fetch.p...

问题:

I'm using a Json in my HTML to get data from Database but I can't access the data of returned Json here is my HTML function :

$.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";
        }
    }
});

and here is fetch.php function :

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));
}

Sorry for this question I'm just new in HTML and Javascript , so anyone know why timestamp won't be set in 'generation_date' element? any help will be much appreciated


回答1:

In your PHP, $output seems to be an array. So in your javascript you need to access on the good index to get the data.

Try :

 document.getElementById("generation_date").textContent = data.certInfo[0].timestamp;
 ----------------------------------------------------------------------^^^
  • 发表于 2019-02-21 13:08
  • 阅读 ( 235 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除