2

我无法将正确编码的 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'));
4

1 回答 1

1

您使用的参数映射取自使用 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}]
于 2013-08-03T04:37:32.937 回答