为什么 fullcalendar 无法解析 JSON?
Why does fullcalendar fail to parse JSON?
总结
我已经设置了全日历,我正尝试使用 JSON 事件源在其中显示数据,如他们的文档 here.
中所述
我不断收到以下失败消息 Failure parsing JSON
。
我尝试过的事情
这是将触发失败消息的 JSON [{"title":"Lorem Ipsum","start":"2019-04-01","end":"2019-04-02"},{"title":"The Test","start":"2018-09-01","end":"2018-09-02"}]
我正在使用 fullcalendar 版本 4.0.2。
我已经在 linter 中验证了 json 我的 PHP 代码 return。
我在 json 响应中添加了 Content-Type: application/json
header。
我已经尝试使用 eventDataTransform
挂钩 return 在完整日历文档 here 中找到的一些示例 JSON (请参阅编辑历史中的代码)
~~奇怪的是,当我将上面的 JSON 直接放在 events
选项的 javascript 中时,它确实起作用了。~~ 编辑:正如所指出的Jaromanda X 和 Quentin 这是一个 javascript 数组而不是 JSON。
代码
var calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [ 'dayGrid' ],
defaultView: 'dayGridMonth',
locale: 'nl',
events: '/fullcalendar/json.php'
});
我希望我的 json 可以被解析,因为响应与我直接提供给 events
选项的响应相同
附加信息
json.php 文件的内容
<?php
header('Content-Type: application/json');
echo json_encode([
[
'title' => 'Lorem Ipsum',
'start' => '2019-04-01',
'end' => '2018-04-02'
],
[
'title' => 'The Test',
'start' => '2018-09-01',
'end' => '2018-09-02'
]
]);exit;
我尝试将方法更改为 GET,但没有帮助。
我附上了在检查器的网络选项卡中看到的响应的屏幕截图JSON response in inspector
我在 php 页面中尝试了您的代码,并以 events: '/fullcalendar/json.php'
的形式调用了完整日历,我不知道是什么给您带来了问题。
完美运行。
<?php
header('Content-Type: application/json');
echo json_encode([
[
'title' => 'Lorem Ipsum',
'start' => '2019-04-16T13:00:00',
'end' => '2019-04-17T14:00:00'
],
[
'title' => 'The Test',
'start' => '2019-04-16T10:00:00',
'end' => '2019-04-17T13:00:00'
]
]);
但是也要考虑你的约会对象。您的起点是 2019 年,终点是 2018 年。还要插入时间以查看您的活动所在。
恐怕我的解决方案可能对其他人没有多大用处,但在这里。
错误是由于我们的主js文件中有如下一行js代码造成的。
if($('table').length > 0){
$('table').wrap('<div class="table-scroll"></div>');
}
这一行弄乱了 fullcalender 生成的 HTML 输出,然后引发了 failure parsing json
错误。
总结
我已经设置了全日历,我正尝试使用 JSON 事件源在其中显示数据,如他们的文档 here.
中所述我不断收到以下失败消息 Failure parsing JSON
。
我尝试过的事情
这是将触发失败消息的 JSON [{"title":"Lorem Ipsum","start":"2019-04-01","end":"2019-04-02"},{"title":"The Test","start":"2018-09-01","end":"2018-09-02"}]
我正在使用 fullcalendar 版本 4.0.2。
我已经在 linter 中验证了 json 我的 PHP 代码 return。
我在 json 响应中添加了 Content-Type: application/json
header。
我已经尝试使用 eventDataTransform
挂钩 return 在完整日历文档 here 中找到的一些示例 JSON (请参阅编辑历史中的代码)
~~奇怪的是,当我将上面的 JSON 直接放在 events
选项的 javascript 中时,它确实起作用了。~~ 编辑:正如所指出的Jaromanda X 和 Quentin 这是一个 javascript 数组而不是 JSON。
代码
var calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [ 'dayGrid' ],
defaultView: 'dayGridMonth',
locale: 'nl',
events: '/fullcalendar/json.php'
});
我希望我的 json 可以被解析,因为响应与我直接提供给 events
选项的响应相同
附加信息
json.php 文件的内容
<?php
header('Content-Type: application/json');
echo json_encode([
[
'title' => 'Lorem Ipsum',
'start' => '2019-04-01',
'end' => '2018-04-02'
],
[
'title' => 'The Test',
'start' => '2018-09-01',
'end' => '2018-09-02'
]
]);exit;
我尝试将方法更改为 GET,但没有帮助。
我附上了在检查器的网络选项卡中看到的响应的屏幕截图JSON response in inspector
我在 php 页面中尝试了您的代码,并以 events: '/fullcalendar/json.php'
的形式调用了完整日历,我不知道是什么给您带来了问题。
完美运行。
<?php
header('Content-Type: application/json');
echo json_encode([
[
'title' => 'Lorem Ipsum',
'start' => '2019-04-16T13:00:00',
'end' => '2019-04-17T14:00:00'
],
[
'title' => 'The Test',
'start' => '2019-04-16T10:00:00',
'end' => '2019-04-17T13:00:00'
]
]);
但是也要考虑你的约会对象。您的起点是 2019 年,终点是 2018 年。还要插入时间以查看您的活动所在。
恐怕我的解决方案可能对其他人没有多大用处,但在这里。
错误是由于我们的主js文件中有如下一行js代码造成的。
if($('table').length > 0){
$('table').wrap('<div class="table-scroll"></div>');
}
这一行弄乱了 fullcalender 生成的 HTML 输出,然后引发了 failure parsing json
错误。