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

Using DataSource to return single string result from web service

1 Answer 413 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Tom asked on 04 Jun 2012, 06:45 AM
I've got a page that uses as WCF web service to do some back-end processing. I can do it quite happily using a standard AJAX call. But I would like to do a similar thing using a Kendo DataSource. The test web service I'm calling simply takes a single argument called "param" and returns it back as: 
public string TestCallParam(string param)
{
    return "you passed: " + param;
}

Here is  the standard ajax call. Both the success and error functions display things for me properly. The data comes back as json data, with a single value called "TestCallParamResult". I can see this using fiddler as well as follows.

{"TestCallParamResult":"you passed: HI"}

$(document).ready(function () {
    $("#TextBox1").text("Starting");
    var args = new Object();
    args.param = "HI";
 
    $.ajax({
        type: "POST",
        url: "cccjq.svc/TestCallParam",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(args),
        dataType: "json",
        processdata: false,
        success: function (msg) {
            alert(msg.TestCallParamResult);
        },
        error: ServiceFailed
    });
 
})
 
function ServiceFailed(result) {
    alert('Service call failed.\r\nStatus = ' + result.status + '.\r\n' + result.statusText);
}

Here is my attempt at doing it using a datasource. I can see the data coming back in fiddler, but I'm probably wrong in the way I've defined the schema or am trying to access the result. I would like to see the alert actually display the contents of the returned string. I should add that Javascript & jquery are not my first language!

$(document).ready(function () {
    $("#TextBox1").text("Starting");
    var args = new Object();
    args.param = "HI";
 
    var ds = new kendo.data.DataSource({
        transport: {
            read: {
                type: "POST",
                url: "cccjq.svc/TestCallParam",
                contentType: "application/json; charset=utf-8",
                dataType: 'json'
            },
            parameterMap: function (options, operation) {
                return JSON.stringify(args);
            }
        },
        schema: {
            data: "TestCallParamResult",
            type: 'json'
        },
        change: function (e) {
            alert(x);
        },
        error: function (e) {
// dont know if this is right either 
            alert('Service call failed.\r\nStatus = ' + e.status + '.\r\n' + e.statusText); 
        }
    });
 
    ds.read();

One other thing on this - the service is called with or without the ds.read() call, is that normal?

Thanks,
Tom

1 Answer, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 05 Jun 2012, 09:39 AM
Hello Tom,

I am afraid that this scenario is not supported out of the box, because the DataSource works with arrays. What I can suggest is either to modify the service to return the JSON object as an array
[{"TestCallParamResult":"you passed: HI"}]

or to use the data function of the schema to construct one:
schema: {
    data: function(data) {
        return [data]
    }         
}
 
Regarding your second question, the dataSource will be read automatically if a widget is bound to it, else you have to manually trigger the operation via the read method. If you do not want the widget to trigger reading, you could set up the autoBind option to false (the default value is true).

All the best,
Alexander Valchev
the Telerik team
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
Tom
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Share this question
or