简体   繁体   English

Kendo UI网格更新

[英]Kendo UI grid update

I'm using a Kendo UI grid with a service that requires the POST request for updating a row to be a JSON string instead of an URL encoded form. 我正在使用一个Kendo UI网格,该服务要求POST请求将行更新为JSON字符串而不是URL编码形式。

My grid dataSource configuration looks like this: 我的网格dataSource配置如下所示:

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);
            }
        }
    },
    //...
});

However the body of request ends up looking like this: 然而,请求的主体最终看起来像这样:

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

An encoded json object ( {"Id": 1, "Foo": "foo", "Bar": "bar"} ) (what encoding is it, btw?) plus the form data. 一个编码的json对象( {"Id": 1, "Foo": "foo", "Bar": "bar"} )(它是什么编码,顺便说一下?)加上表单数据。

Doing it directly with jQuery works just fine: 直接用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);}
});

According to the documentation , I should be able to set update as a function and call that, but apparently this hasn't work since 2012 . 根据文档 ,我应该能够将更新设置为函数并调用它,但显然自2012年以来没有用

How can I make Kendo post the correct data? 如何让Kendo发布正确的数据?

Its not intuitive or well documented, but you want to call JSON.stringify() in the parameterMap function: 它不直观或记录良好,但您想在parameterMap函数中调用JSON.stringify()

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" }
            }
        }
    }
});

尝试使用

JSON.stringify(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM