如何使用json发送html


How to send html using json

我正在为wordpress创建一个插件,我想管理一所学院的课程。我使用fullcalendar来显示数据库中的记录,使用json。问题是,我想在每个事件上创建编辑和删除按钮(简单链接)。我试图用json发送html代码,但我在浏览器页面中得到了作为文本的html标记。

有没有办法将工作html添加到我的json中

<?php
//json response view lessons
add_action('wp_ajax_utt_json_calendar','utt_json_calendar');
function utt_json_calendar(){
    global $wpdb;
    $viewType = $_POST['viewType'];
    $viewFilter = $_POST['viewFilter'];
    $lessonsTable = $wpdb->prefix."utt_lessons";
    $lessons = $wpdb->get_results("SELECT * FROM $lessonsTable WHERE classroomID=$viewFilter;");
    $jsonResponse = array();
    foreach($lessons as $lesson){
        $result[title] = $lesson->lessonID."<a href='#'>asd</a>";
        $result[start] = $lesson->datetime;
        array_push($jsonResponse,$result);
    }
    echo json_encode($jsonResponse);
    die();
}
?>

Fullcalendar查询该php脚本,并期望event.title文本

如果你想为你的活动添加链接,你需要在FullCalendar选项(Javascript)中这样做。

您要查找的FullCalendar选项是eventRender。它应该看起来像:

eventRender: function(event, element) {
    element.find('.fc-title').append($("<a href='#'>"+event.editlink+"</a>"));
}

它看起来像小提琴。

在PHP中,将属性editLink(或您想命名的任何名称)添加到返回的JSON:中

foreach($lessons as $lesson){
    $result[title] = $lesson->lessonID;
    $result[start] = $lesson->datetime;
    $result[editLink] = "asd"; //you could use HTML here but it's better in JS
    array_push($jsonResponse,$result);
}

实际上没有任何方法可以只在PHP中执行您想要的操作,因为您必须修改FullCalendar选项(这必须在JS中完成).