2013-08-02 31 views
2

我无法将正确编码的json字符串作为对象传递给php文件。什么是Kendoui parameterMap解码php对象的正确的json编码?

去关闭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); 

This returns从这个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}]} 

问题是什么我做错了,我要修改正在传递的字符串。看来,它应该只是作为我可以简单地通过

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

回答

1

您所使用的参数图是从使用JSONP终点剑道在线演示所运行正确编码的JSON元素传递。你的情况这将是一个更容易这样的:

 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}] 
+0

谢谢...那肯定是有帮助,使更多的感。我是否正确地假设没有得到解码POST这个代码:'$ request = preg_replace(“/%u([0-9a-f] {3,4})/ i”,“&#x\\1;”, urldecode($请求)); $ request = html_entity_decode($ request,null,'UTF-8');' –