簡體   English   中英

使用javascript從php獲取json數據

[英]Get json data from php using javascript

我是json的新手。我從json格式的php代碼中獲取數據。

[
    {
        "Title": "New Event",
        "TYPE": "info",
        "StartsAt": "16 November 201512:00",
        "EndsAt": "25 November 201512:00"
    },
    {
        "Title": "Party",
        "TYPE": "warning",
        "StartsAt": "25 November 2015 09:30",
        "EndsAt": "25 November 2015 5:30"
    },

]

我有一個JavaScript文件demo.js

我想在js文件中接收此數據,目前該數據已進行硬編碼。 我想展示我從數據庫獲取的事件。

vm.calendarView = 'month';
vm.calendarDay = new Date();

 vm.events = [
  {
    title: 'An event',
    type: 'warning',
    //startsAt: moment().startOf('week').subtract(2, 'days').add(8, 'hours').toDate(),
    //endsAt: moment().startOf('week').add(1, 'week').add(9, 'hours').toDate(),
    startsAt:new Date(2015,10,1,1),
    endsAt:new Date(2013,5,1,1),
    draggable: true,
    resizable: true
  }, {
    title: '<i class="glyphicon glyphicon-asterisk"></i> <span class="text-primary">Another event</span>, with a <i>html</i> title',
    type: 'info',
    startsAt: moment().subtract(1, 'day').toDate(),
    endsAt: moment().add(5, 'days').toDate(),
    draggable: true,
    resizable: true
  }, {
    title: 'This is a really long event title that occurs on every year',
    type: 'important',
    startsAt: moment().startOf('day').add(7, 'hours').toDate(),
    endsAt: moment().startOf('day').add(19, 'hours').toDate(),
    recursOn: 'year',
    draggable: true,
    resizable: true
  }
];

如果問題大致是“我如何從數據庫中獲取數據並以Javascript將其輸出為JSON”,那么希望以下偽代碼將指導您朝正確的方向發展。

<?php
    include 'db.php';

    $json=array();

    /* Query the db */
    $sql='select * from `events`;';
    $res=$db->query( $sql );
    if( $res ){
        while( $rs=$res->fetch_object() ){
            $json[]=array(
                'title'     =>  $rs->title,
                'type'      =>  $rs->type,
                'startsAt'  =>  $rs->startsAt,
                'endsAt'    =>  $rs->endsAt,
                'draggable' =>  $rs->draggable,
                'resizable' =>  $rs->resizable
            );  
        }
    }
    $db->close();
    $js_json_var=json_encode( $json, JSON_FORCE_OBJECT );       
?>

<html>
    <head>
        <title></title>
        <script>
            var json=<?php echo $js_json_var;?>;
            /* Other js code */
        </script>
    </head>
    <body>
        <h1>json</h1>
    </body>
</html>

Javascript:AJAX請求:

var events;
$.ajax({
   url: '/data.php',
   success: function(data){
         events = JSON.parse(data);
   }
});

data.php是這樣的

$db = new PDO(/* init connection here */);
print json_encode($db->query('select * from `tablename`')->fetchAll(PDO::FETCH_ASSOC));
   // In javascript
    $.ajax({
    type: 'post',
    url: 'data.php',
    data: 'data',
    cache: false,
    success: function(data)
           {
             console.log(data);
           }
    });


    // In data.php

    header('Content-type: application/json');

    $data = array();
    $sql = "SELECT * FROM TABLE_NAME";
    $res = mysql_query($sql);
    while($row = mysql_fetch_assoc($res))
    {
      $data[] = $row;
    }
    echo json_encode($data, true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM