我在我的C#项目中使用了一个来自http://gantt.twproject.com/distrib/gantt.html的甘特图。如果单击导出按钮,我们会从甘特图中获取json。我将同样的json传递给我的wcf,如下所示。
function WCFJSON(ganttData) {
console.log(ganttData); //Object
var gdata = JSON.stringify(ganttData);
console.log(gdata);
$.ajax({
type: "POST",
url: "../../GanttService.svc/SaveGanttData", // Location of the service
data: gdata, //Data sent to server
contentType: "application/json; charset=utf-8",
dataType: "json", //Expected data format from server
processdata: true, //True or False
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
这是gdata变量中的json。
"{"tasks":[{"id":-1,"name":"Gantt editor","code":"","level":0,"status":"STATUS_ACTIVE","start":1386527400000,"duration":30,"end":1390415399999,"startIsMilestone":true,"endIsMilestone":false,"collapsed":false,"assigs":[{"id":"tmp_1386591686448","resourceId":"tmp_1","roleId":"tmp_1","effort":0}],"description":"","progress":0},{"id":-2,"name":"codings","code":"","level":1,"status":"STATUS_ACTIVE","start":1386527400000,"duration":10,"end":1387564199999,"startIsMilestone":false,"endIsMilestone":false,"collapsed":false,"assigs":[],"description":"","progress":0},{"id":-3,"name":"gant parts","code":"","level":2,"status":"STATUS_ACTIVE","start":1386527400000,"duration":2,"end":1386700199999,"startIsMilestone":false,"endIsMilestone":false,"collapsed":false,"assigs":[],"depends":""},{"id":-4,"name":"editor parts","code":"","level":2,"status":"STATUS_SUSPENDED","start":1386700200000,"duration":4,"end":1387218599999,"startIsMilestone":false,"endIsMilestone":false,"collapsed":false,"assigs":[],"depends":"3"},{"id":-5,"name":"testings","code":"","level":1,"status":"STATUS_SUSPENDED","start":1388601000000,"duration":15,"end":1390415399999,"startIsMilestone":false,"endIsMilestone":false,"collapsed":false,"assigs":[],"depends":"2:5","description":"","progress":0},{"id":-6,"name":"test on safari","code":"","level":2,"status":"STATUS_SUSPENDED","start":1389810600000,"duration":2,"end":1389983399999,"startIsMilestone":false,"endIsMilestone":false,"collapsed":false,"assigs":[],"depends":""},{"id":-7,"name":"test on ie","code":"","level":2,"status":"STATUS_SUSPENDED","start":1390156200000,"duration":3,"end":1390415399999,"startIsMilestone":false,"endIsMilestone":false,"collapsed":false,"assigs":[],"depends":"6"},{"id":-8,"name":"test on chrome","code":"","level":2,"status":"STATUS_SUSPENDED","start":1390156200000,"duration":2,"end":1390328999999,"startIsMilestone":false,"endIsMilestone":false,"collapsed":false,"assigs":[],"depends":"6"}],"selectedRow":2,"deletedTaskIds":[],"resources":[{"id":"tmp_1","name":"Resource 1"},{"id":"tmp_2","name":"Resource 2"},{"id":"tmp_3","name":"Resource 3"},{"id":"tmp_4","name":"Resource 4"},{"id":"tmp_5","name":"Resource 5"},{"id":"tmp_6","name":"Resource 6"},{"id":"tmp_7","name":"Resource 7"},{"id":"tmp_8","name":"Resource 8"},{"id":"tmp_9","name":"Resource 9"},{"id":"tmp_10","name":"Resource 10"}],"roles":[{"id":"tmp_1","name":"Project Managers"},{"id":"tmp_2","name":"Worker"},{"id":"tmp_3","name":"Stakeholder/Customer"}],"canWrite":true,"canWriteOnParent":true}"
我的WCF方法是
public string SaveGanttData(String tasks)
{
try
{
var apps = JsonConvert.DeserializeObject<Tasks>(tasks);
var retValue = "";
foreach (var task in apps.tasks)
{
retValue += "Name - " + task.name + " Code - " + task.code;
}
Console.ReadLine();
return string.Format("You entered: {0}", retValue);
}
catch (Exception ex)
{
return string.Format("Failed: {0}", ex.Message);
}
}
在服务命中后,浏览器控制台中始终显示null。我已经尝试了几种方法,但无法将确切的json数据传递给我的wcf。请帮帮我!
答案 0 :(得分:0)
您的服务仅接受单string
,而您正在发送更复杂的JSON格式:
tasks
- 对象数组selectedRow
- 整数deletedTaskIds
- 对象数组resources
- 对象数组roles
- 对象数组canWrite
- 布尔canWriteOnParent
- 布尔如果您使用的是Visual Studio 2013,请将JSON复制到剪贴板并使用菜单编辑&gt;将其粘贴到Visual Studio中。 Paste Special &gt; 将JSON粘贴为类以查看从C#透视图表示此JSON的正确类。