FullCalendar无法加载CodeIgniter数据问题求助
Let's walk through the most likely reasons your FullCalendar isn't displaying events from your CodeIgniter API, along with practical fixes for each scenario:
1. Check for Invalid or Impure JSON Output
FullCalendar depends entirely on valid, unadulterated JSON to render events. Echoing JSON directly from a view can easily introduce hidden problems:
- Extra whitespace, newlines, or accidental HTML snippets (from unclosed PHP tags or leftover template code) will break JSON parsing.
- PHP errors (like database connection failures or undefined variables) might inject error messages into the response, making it invalid.
Fix Steps:
- Directly visit your endpoint URL (
https://panel.strater.com.au/index.php?staff/load/) in a browser. Copy the output and test it with a JSON validator (you can use your browser's console: runJSON.parse('your-output-here')to check for parsing errors). - Move the JSON generation logic to your
Staffcontroller instead of a view for better control. Example controller code:public function load() { // Fetch institution ID from session $institution_id = $this->session->userdata('institution_id'); $events = []; if ($institution_id) { // Get projects from database $projects = $this->db->get_where('project', ['institution_id' => $institution_id])->result_array(); // Format events for FullCalendar foreach ($projects as $row) { $events[] = [ 'id' => $row["project_code"], 'title' => $row["description"], // Use ISO 8601 format (FullCalendar's most reliable date format) 'start' => date('c', $row["timestamp_start"]), 'end' => date('c', $row["timestamp_end"]) ]; } } // Set proper JSON header and output clean JSON $this->output ->set_content_type('application/json') ->set_output(json_encode($events)); } - If you must use a view, ensure there are no spaces/newlines before
<?phpand omit the closing?>entirely to avoid trailing whitespace.
2. Verify Date Format Compatibility
Your current date format (d-M-Y H:i:s) might not be parsed correctly by FullCalendar. While it can handle some human-readable formats, ISO 8601 (e.g., 2024-05-20T14:30:00) is the most universally supported.
Fix:
Replace your date() calls with date('c', $timestamp) as shown in the controller example above—this generates a format FullCalendar understands natively.
3. Check for Session or Database Query Issues
If your institution_id session variable is missing or invalid, your database query will return no results, resulting in an empty JSON array (and no events).
Fix Steps:
- Temporarily add debug output to your endpoint to confirm the session value:
// Add this at the top of your controller method var_dump($this->session->userdata('institution_id')); exit; - Run the query directly in your database (replace
{valid-institution-id}with a known valid value) to ensure it returns data:SELECT * FROM project WHERE institution_id = {valid-institution-id};
4. Inspect Network Requests with Browser Dev Tools
Use your browser's Developer Tools (Network tab) to audit the request to your endpoint:
- Status Code: Ensure it returns
200 OK(not 404, 500, or 403). A 500 error means there's a server-side PHP issue—check CodeIgniter's error logs for details. - Response Content: Confirm the response is pure JSON with no error messages or extra text.
- CORS Issues: If your calendar page is hosted on a different domain than the API, you'll see a CORS error. Fix this by adding CORS headers to your controller:
$this->output ->set_header('Access-Control-Allow-Origin: https://your-calendar-domain.com') // Restrict to your domain for security ->set_content_type('application/json') ->set_output(json_encode($events));
5. Validate FullCalendar Initialization
Double-check your FullCalendar code for typos or misconfigurations:
- Ensure the
eventsURL has no typos in controller/method names. - Confirm the
#calendarelement exists on the page before initialization (your$(document).ready()wrapper handles this, but it's worth verifying withconsole.log($('#calendar').length)).
内容的提问来源于stack exchange,提问作者Taylan Selvi

