如何修复使用FullCalendar.js解析JSON失败的问题

时间:2019-06-13 01:27:51

标签: javascript jquery fullcalendar

我对完整日历有疑问,我目前是新来的,为什么我的回答显示出来。我目前从我的php文件的网址获得响应。我有一个模块,需要在其中获取日历上的所有事件,因此我决定使用完整日历,因此现在遇到此错误。

  

消息:“无法解析JSON”

我将与大家分享我编写的代码。

前端

<script>

  document.addEventListener('DOMContentLoaded', function() {
  var calendarEl = document.getElementById('calendar');

      var calendar = new FullCalendar.Calendar(calendarEl, {
        plugins: [ 'interaction', 'dayGrid', 'timeGrid' ],
        defaultView: 'dayGridMonth',
        defaultDate: '2019-06-13',
        header: {
          left: 'prev,next ',
          right: 'title',

        },
        events:'./Function/fetch_calendar_schedule.php',
        eventColor: '#008B74',
        eventTextColor: '#ffff'
      });

      calendar.render();
    });



</script>

后端:

    <?php 
    require_once('../ConnectionString/require_db.php');
    session_start();

    $calendar_content = $db->prepare('SELECT event_name,event_start_date,event_end_date,link FROM tblevents')
    or die($db->error);
    $calendar_content->execute();
    $calendar_content->bind_result($title,$start,$end,$link);

    $result = array();
    while($calendar_content->fetch()) {
        //validate using user type where user redirect

        $result[] = array('title'=>$title,'start'=>$start,'end'=>$end,'url'=>$link);
        header('Content-type: application/json');
        echo json_encode($result);


    }

?>

响应结果:

[{"title":"<strong>42nd Annual Convention <\/strong>\r\nSMX MOA\r\nNovember 15, 2019\r\nScientific Chairperson: Dr. Panaligan\r\nCoordinators: Dr. Rowena Zabat","start":"0000-00-00 00:00:00","end":"0000-00-00 00:00:00","url":""}][{"title":"<strong>42nd Annual Convention <\/strong>\r\nSMX MOA\r\nNovember 15, 2019\r\nScientific Chairperson: Dr. Panaligan\r\nCoordinators: Dr. Rowena Zabat","start":"0000-00-00 00:00:00","end":"0000-00-00 00:00:00","url":""},{"title":"Download Registration Form (PDF)","start":"0000-00-00 00:00:00","end":"0000-00-00 00:00:00","url":"http:\/\/www.psmid.org\/forms\/pdf\/42nd-annual-registration.pdf"}][{"title":"<strong>42nd Annual Convention <\/strong>\r\nSMX MOA\r\nNovember 15, 2019\r\nScientific Chairperson: Dr. Panaligan\r\nCoordinators: Dr. Rowena Zabat","start":"0000-00-00 00:00:00","end":"0000-00-00 00:00:00","url":""},{"title":"Download Registration Form (PDF)","start":"0000-00-00 00:00:00","end":"0000-00-00 00:00:00","url":"http:\/\/www.psmid.org\/forms\/pdf\/42nd-annual-registration.pdf"},{"title":"Independence Day","start":"2019-06-12 00:00:00","end":"2019-06-12 00:00:00","url":""}]

1 个答案:

答案 0 :(得分:0)

为了解决问题,我将json_encode结果放在循环之外:

    <?php 
    require_once('../ConnectionString/require_db.php');
    session_start();

    $calendar_content = $db->prepare('SELECT event_name,event_start_date,event_end_date,link FROM tblevents')
    or die($db->error);
    $calendar_content->execute();
    $calendar_content->bind_result($title,$start,$end,$link);

    $result = array();
    while($calendar_content->fetch()) {
        //validate using user type where user redirect

        $result[] = array('title'=>$title,'start'=>$start,'end'=>$end,'url'=>$link);
        header('Content-type: application/json');



    }
    echo json_encode($result);
?>