Kendo UI网格更新

时间:2014-07-17 21:43:13

标签: jquery kendo-ui

我正在使用一个Kendo UI网格,该服务要求POST请求将行更新为JSON字符串而不是URL编码形式。

我的网格数据源配置如下所示:

dataSource: new kendo.data.DataSource({
    transport: {
        read: "/Service.svc/instructors",
        update: {
            url: "/Service.svc/instructors",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: function (data) {
                return kendo.stringify(data);
            }
        }
    },
    //...
});

然而,请求的主体最终看起来像这样:

0=%7B&1=%22&2=I&3=d&4=%22&5=:&6=%20&7=1&8=,&9=%20&10=%22&11=F&12=o&13=o&14=%22&15=:&16=%20&17=%22&18=f&19=o&20=o&21=%22&22=,&23=%20&24=%22&25=B&26=a&27=r&28=%22&29=:&30=%20&31=%22&32=b&33=a&34=r&35=%22&36=%7D&Id=1&Foo=foo&Bar=bar

编码的json对象({"Id": 1, "Foo": "foo", "Bar": "bar"})(它是什么编码,顺便说一下?)加上表单数据。

直接使用jQuery可以正常工作:

$.ajax({
    type: "POST", 
    url: "/Service.svc/instructors", 
    data: kendo.stringify({Id: 1, Foo: "foo", Bar: "bar"}),
    contentType:"application/json; charset=utf-8",
    dataType: "json",
    success: function(data) { console.log(data);}
});

根据documentation,我应该能够将更新设置为函数并调用它,但显然是hasn't work since 2012

如何让Kendo发布正确的数据?

2 个答案:

答案 0 :(得分:8)

它不直观或记录良好,但您想在JSON.stringify()函数中调用parameterMap

var productsDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: "/Products"
        },
        update: {
            type: "POST",
            url: "/Products/Edit",
            dataType: "json",
            contentType: "application/json"
        },
        parameterMap: function(data, operation) {
            if (operation === "update" || operation === "create") {
                return JSON.stringify({product: data});
            }
            return data;
        }
    },
    schema: {
        model: {
            id: "ProductID",
            fields: {
                Name: { type: "string" },
                ListPrice: { type: "number" },
                SellStartDate: { type: "date" }
            }
        }
    }
});

答案 1 :(得分:0)

尝试使用

JSON.stringify(data);