This is a migrated thread and some comments may be shown as answers.

Post To .NET WebMethod ScriptMethod throws Javascript error

1 Answer 50 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 15 Jul 2015, 08:48 AM
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}}

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 16 Jul 2015, 10:02 AM
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!
 
Tags
Data Source
Asked by
Joseph
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or