kendoui parameterMap解码php对象的正确json编码是什么


What is the proper json encoding for the kendoui parameterMap to decode php object?

我在将正确编码的json字符串作为对象传递到php文件时遇到问题。

离开kendoui的例子,我有一个数据源,它是这样实例化的:

$("#scheduler").kendoScheduler({
    date: new Date("2013/7/30"),
    startTime: new Date("2013/7/30 07:00 AM"),
    views: [
                "day",
                "week",
                { type: "month", eventHeight: 20, selected: true },
                "agenda"
            ],
    timezone: "Etc/UTC",
    height: $(document).height()-72,
    dataSource: {
        batch: true,
        transport: {
            read: {
                url: "scheduler_data_pdo.php?type=read",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                dataType: "json"
            },
            update: {
                url: "scheduler_data_pdo.php?type=update",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                dataType: "json"
            },
            create: {
                url: "scheduler_data_pdo.php?type=create",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                dataType: "json"
            },
            destroy: {
                url: "scheduler_data_pdo.php?type=destroy",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                if (operation !== "read" && options.models) {
                    return {models: kendo.stringify(options.models)};
                }
            }
        },
        schema: {
            model: {
                id: "taskId",
                fields: {
                    taskId: { from: "taskId", type: "number" },
                    title: { from: "title", defaultValue: "No title", validation: { required: true } },
                    start: { type: "date", from: "start" },
                    end: { type: "date", from: "end" },
                    startTimezone: { from: "startTimezone" },
                    endTimezone: { from: "endTimezone" },
                    description: { from: "description" },
                    recurrenceId: { from: "recurrenceId" },
                    recurrenceRule: { from: "recurrenceRule" },
                    recurrenceException: { from: "recurrenceException" },
                    ownerId: { from: "ownerId", defaultValue: 1 },
                    isAllDay: { type: "number", from: "isAllDay" }
                }
            }
        },
    }
});

这里的更新结果是:

models=[{"title":"No title","start":"2013-07-17T00:00:00.000Z","startTimezone":"","end":"2013-07-17T00:00:00.000Z","endTimezone":"","recurrenceRule":"","recurrenceException":"","isAllDay":true,"description":"","taskId":0,"recurrenceId":"","ownerId":1}]

这是不正确的JSON。。。。为了进行清理,我在php端通过以下代码运行它:

header("Content-type: application/json");
$request = file_get_contents('php://input');
$request = preg_replace("/%u([0-9a-f]{3,4})/i","&#x''1;",urldecode($request)); 
$request = html_entity_decode($request,null,'UTF-8');
$request = ltrim($request,"models=");
$request = '{"models":'.$request.'}';
$request =json_decode($request);

这将从JSON字符串返回一个正确编码的php对象:

{"models":[{"title":"No title","start":"2013-07-17T00:00:00.000Z","startTimezone":"","end":"2013-07-17T00:00:00.000Z","endTimezone":"","recurrenceRule":"","recurrenceException":"","isAllDay":true,"description":"","taskId":0,"recurrenceId":"","ownerId":1}]}

问题是我做错了什么,我必须修改正在传递的字符串。它似乎应该作为一个正确编码的JSON元素传递,我可以简单地通过运行它

$request = json_decode(file_get_contents('php://input'));

您正在使用的参数映射取自使用JSONP端点的Kendo在线演示。在你的情况下,这样做会容易得多:

        parameterMap: function(options, operation) {
            if (operation !== "read" && options.models) {
                return kendo.stringify(options.models);
            }
            return kendo.stringify(options);
        }

这将把"模型"作为一个有效的JSON数组发送:

[{"title":"No title","start":"2013-07-17T00:00:00.000Z","startTimezone":"","end":"2013-07-17T00:00:00.000Z","endTimezone":"","recurrenceRule":"","recurrenceException":"","isAllDay":true,"description":"","taskId":0,"recurrenceId":"","ownerId":1}]