Post To .NET WebMethod ScriptMethod throws Javascript error

2 posts, 0 answers
  1. Joseph
    Joseph avatar
    11 posts
    Member since:
    Jul 2015

    Posted 15 Jul 2015 Link to this post

    Hi,

    I'm trying to fetch some json data from a .NET WebMethod (ScriptMethod) via a HTTP POST using a kendo.data.DataSource.

    I was able to trigger the .NET WebMethod and json data is returned.

    However, a javascript error "Uncaught TypeError: e.slice is not a function" ​is thrown by "kendo.all.min.js:11" on the client side.

    Not sure what is wrong.


    The javascript code is as follows:

               var param = { ID: 1, SearchString: "some string" };

                var dataSourceEntities = new kendo.data.DataSource({
                    transport: {
                        read: {
                            type: "POST",
                            url: app.apiUrl + "/GetEntities",
                            contentType: "application/json; charset=utf-8",
                            dataType: 'json'
                        },
                        parameterMap: function(options, operation) {
                            return JSON.stringify(param);
                        },
                        schema: {
                            data: "d.Data", // ASMX services return JSON in the following format { "d": <result> }. Specify how to get the result.
                            total: "d.Total",
                            model: { // define the model of the data source. Required for validation and property types.
                                id: "SelectOptionVM",
                                fields: {
                                    text: { type: "number" },
                                    value: { type: "string" },
                                    selected: { type: "boolean" }
                                }
                            }
                        }
                    }
                }).read();


    The .NET WebMethod code is as follows:

            [System.Web.Services.WebMethod(EnableSession = true)]
            [System.Web.Script.Services.ScriptMethod]
            public static DataSourceResult GetEntities(int ID, string SearchString)
            {
                List<SelectOptionVM> result = new List<SelectOptionVM>();

                List<TestEntity> tes =
                    ProctorSessions
                    .Select(ts => ts.Location.Entity)
                    .Distinct()
                    .OrderBy(te => te.Name).ToList();

                foreach (TestEntity te in tes)
                {
                    result.Add(new SelectOptionVM() { text = te.Name, value = te.TestEntityId.ToString() });
                }

                return result.AsQueryable().ToDataSourceResult(int.MaxValue, 0, null, null);
            }

     

    The JSON data returned (captured on Fiddler) is as follows:

    {"d":{"__type":"Zoomorphix.Kendo.Extensions.DataSourceResult","Data":[{"__type":"ExamStudioPortal.Proctor.Dashboard4+SelectOptionVM","text":"Entity 1","value":"1","selected":false},{"__type":"ExamStudioPortal.Proctor.Dashboard4+SelectOptionVM","text":"Entity 2","value":"2","selected":false},{"__type":"ExamStudioPortal.Proctor.Dashboard4+SelectOptionVM","text":"Entity 3","value":"3","selected":false}],"Total":3}}
  2. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 16 Jul 2015 Link to this post

    Hi,

    The problem will occur because the schema is set to the transport options and so it will not be taken into account. The schema should be set to the dataSource options:
    var dataSourceEntities = new kendo.data.DataSource({
        transport: {
            read: {
                type: "POST",
                url: app.apiUrl + "/GetEntities",
                contentType: "application/json; charset=utf-8",
                dataType: 'json'
            },
            parameterMap: function(options, operation) {
                return JSON.stringify(param);
            }
        },
        schema: {
            data: "d.Data", // ASMX services return JSON in the following format { "d": <result> }. Specify how to get the result.
            total: "d.Total",
            model: { // define the model of the data source. Required for validation and property types.
                id: "SelectOptionVM",
                fields: {
                    text: {
                        type: "number"
                    },
                    value: {
                        type: "string"
                    },
                    selected: {
                        type: "boolean"
                    }
                }
            }
        }
    })


    Regards,
    Daniel
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
Back to Top