簡體   English   中英

FullCalendar.io 事件無法通過 PHP 加載,FullCalendar 嘗試解析 PHP 腳本的內容而不是其輸出

[英]FullCalendar.io events can't be loaded through PHP, FullCalendar tries to parse the contents of the PHP script instead of its output

我正在嘗試將 MySQL 數據庫中的一些事件加載到 FullCalendar 的日歷中。 為此,我在 PHP 文件中創建了到數據庫的連接,獲取數據並將其輸出為 JSON。

連接工作正常,在終端中執行 PHP 文件時,我得到一個包含正確數據的 JSON。

但是當試圖通過 FullCalendar API 獲取數據時它不起作用。 在瀏覽器控制台中,我從 FullCalendar 收到警告“解析 JSON 失敗”,當查看它試圖解析的響應時,它只是 php 代碼本身,而不是它的輸出。

所以我復制了我在普通終端中執行 PHP 文件得到的正確輸出,並用它替換了 PHP 文件中的 PHP 代碼,然后解析成功,所以顯然 FullCalendar 只是嘗試解析 PHP 文件的內容其輸出。

這是什么原因? 如何正確地將輸出從 PHP 文件傳輸到 FullCalendar API?

index.html 中的 JavaScript:

<script>

        document.addEventListener('DOMContentLoaded', function() {
            var calendarEl = document.getElementById('calendar');
            
            var calendar = new FullCalendar.Calendar(calendarEl, {
            initialDate: '2021-09-12',
            events: "get-events.php",
            });
            
            calendar.render();
        });

</script>

獲取事件.php

 <?php
    $servername = "127.0.0.1";
    $username = "root";
    $password = "";
    $dbname = "database";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT Title as title, Start as start, End as end FROM event_table";
    $result = mysqli_query($conn, $sql); 
    $myArray = array();
    if ($result -> num_rows > 0) {
        while($row = $result -> fetch_assoc()) {
            $myArray[] = $row;
        }
    } 
    print json_encode($myArray);
?> 

解決方法很簡單,與PHP或JavaScript代碼無關,只是我的服務器環境沒有正確配置(尤其是PHP),因此無法執行PHP代碼。

暫無
暫無

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

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