kendoui parameterMap 解码 php 对象的正确 json 编码是啥?

Posted

技术标签:

【中文标题】kendoui parameterMap 解码 php 对象的正确 json 编码是啥?【英文标题】:What is the proper json encoding for the kendoui parameterMap to decode php object?kendoui parameterMap 解码 php 对象的正确 json 编码是什么? 【发布时间】:2013-08-03 07:40:07 【问题描述】:

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

【问题讨论】:

【参考方案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]

【讨论】:

谢谢...这绝对有帮助并且更有意义。我是否正确假设无法使用以下代码解码 POST:$request = preg_replace("/%u([0-9a-f]3,4)/i","&#x\\1;",urldecode($request)); $request = html_entity_decode($request,null,'UTF-8');

以上是关于kendoui parameterMap 解码 php 对象的正确 json 编码是啥?的主要内容,如果未能解决你的问题,请参考以下文章

[译]kendoui - 方法和事件

Mybatis中parameterType和parameterMap的区别

[转]KendoUI系列:Grid

Mybatis中parameterType和parameterMap的区别

KendoUI 级联下拉列表,需要来自 2 个下拉列表的值。

3.4 ParameterMap and Inline Parameter