我无法将正确编码的json字符串作为对象传递给php文件。
关闭kendoui示例,我有一个像这样实例化的dataSource:
$("#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'));
答案 0 :(得分:1)
您使用的参数地图取自使用JSONP终点的Kendo在线演示。在你的情况下,这样做会容易得多:
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return kendo.stringify(options.models);
}
return kendo.stringify(options);
}
这会将“models”作为有效的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}]