2013-08-02 33 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');' –