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

Problem calling Custom REST apis

2 Answers 385 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Millennium
Top achievements
Rank 1
Millennium asked on 19 Feb 2013, 01:52 PM
Hello,

We are unable to call some of the REST api's via Kendo framework , which we had successfully called via Jquery ajax requests.
Here below given are the
1. C# Code example of the REST API.
2. Code example which can successfully call the REST API
via jquery ajax request.
3. Format which we have used to call  the REST API
from kendo framework.

It would appreciated if you can provide us with the correct Kendo code format to call this REST api.

1. C# Code example of the REST API.

    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    //[AuthenticatingHeader]
    public partial class Account
    {
 
 
        [WebInvoke(Method = "POST", UriTemplate = "DeleteAccount?uniqueaccessid={uniqueaccessid}", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat =
 
WebMessageFormat.Json)]
        public static void DeleteAccount(string uniqueaccessid, string accountId)
        {
            int userid = Accessibility.GetUserId();
            if (userid > 0)
            {
                string output = Tools.ServiceLayer.Account.DeleteAccount(Convert.ToInt32(accountId), Convert.ToInt32(userid)).ToString();
                HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
                HttpContext.Current.Response.Write(output);
            }
        }
    }


2. Code example which can successfully call the REST API
via jquery ajax request. 

function GetTransactionAccount(uniqueaccessid, accountId) {
  var jData = {};
      jData.uniqueaccessid = uniqueaccessid;
      jData.accountId = accountId;
      $.ajax({
          url: RestBaseURL + "Account/"DeleteAccount?uniqueaccessid=" + uniqueaccessid + "",
          data: JSON.stringify(jData),
          dataType: "json",
          type: "POST",
          async: false,
          contentType: "application/json; charset=utf-8",
          cache: false,
          success: function (response) {
              alert(response);
 
          },
          failure: function () { alert("failed"); }
      });
  }


3. Format which we have used to call  the REST API
from kendo framework.


    var changeaccid;
    function GetAccountInfo(uniqueaccessid) {
     
    var accid;
 
               var dataSourceobj = new kendo.data.DataSource({
                transport: {
                     read:{
                        url:RestBaseURL + "Cash/GetAccountInfo?uniqueaccessid=" + uniqueaccessid + "",
                        dataType: "json",
                         type: "POST"
                          },
 
                    destroy:{
                        url: RestBaseURL + "Account/DeleteAccount?uniqueaccessid=" + uniqueaccessid ,
                        //url: RestBaseURL + "Account/DeleteAccount",
                        dataType: "json",
                        type: "POST"
                         },
                    parameterMap: function(options, type) {
                    if (type == "destroy") {
                    return {
                    'uniqueaccessid': uniqueaccessid,
                    'accountId': options.models[0].id
                     
                           };
                                         }
                                                          }
 
                },        
                batch: true,
                pageSize: 10,
                schema: {
                    type: "json",
                    model: {
                        fields: {
                            id : {type: "number"},
                            lender: { type: "string" },
                            id: { type: "string" },
                            rate: { type: "number" },
                            startingbalance: { type: "number" },
                            emergencyfund: { type: "number" },
                            startingdate: { type: "date" },
                            balance: { type: "number" }
                                }
                            }
                        }
                 });      
                   
                 $("#grid2").kendoGrid({
                    dataSource: dataSourceobj,
                    height: 130,
                    selectable: "multiple",
                    change: function() {
                        changeaccid = this.dataItem(this.select()).id;
                    },
                    sortable: true,
                    resizable: true,
                    reorderable: true,
                    filterable: true,
                    columnMenu: true,
                    pageable:true,
                    columns: [
                            { title:"" ,template: "<img src='images/ico/acc_on.png'  title='Green: Consider for Debt eliminator payoff. Red: Not consider
 
for Debt eliminator payoff'/>",width: "25px"},
                            { title:"Id",field: "id" ,width: "30px"},
                            { title:"Bank Name",field: "lender" ,width: "185px"},
                            { title:"A/C Number",field : "accountnumber" ,width: "90px"},
                            { title:"Interest Rate%",field: "rate",format: "{0:N}",width: "110px" },
                            { title:"Beg. Balance",field: "startingbalance", format: "{0:c}",width: "95px"},
                            { title:"Emerg. Fund",field: "emergencyfund" , format: "{0:c}",width: "95px"},
                            { title:"Opening Date",field: "startingdate" ,template: '#= kendo.toString(startingdate,"MM/dd/yyyy") #',width: "100px"},
                            { title:"Balance", field: "balance" , format: "{0:c}",width: "70px"},
                            //{ title:"Action", template: "<img src='images/ico/edit.png' title='Edit'/><img src='images/ico/delete.png'
 
title='Delete'/>",width: "50px"}],
                            {title:"Action", command: ["edit", "destroy"], width: "130px" }],
                    editable: "inline"
                    });
  
 
 
 
    }


Kindly provide us with correct code to call the API.


Thanks in advance

2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 21 Feb 2013, 10:12 AM
Hi,

 The Kendo DataSource is using $.ajax under the hood so providing the same configuration options to transport.read should work. Try passing all settings which you used with $.ajax e.g.

transport: {
     read: {
         url: "...",
         cache: "",
         data: "",
         contentType: "...",
         dataType: "..."
     }
}

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Millennium
Top achievements
Rank 1
answered on 22 Feb 2013, 05:43 AM
Hi Atanas Korchev,

Thanks for the reply.

Sorry that I forgot to mention that the 'read block' works fine.

The problem is with the destroy command, where we need to get  the selected data from Kendo grid model to pass through the "data: " attribute.

How we can get the selected model data to set against the "data: " attribute?

Is the model data only available within parameter map?

UPDATE: The issue is resolved.

We got the solution to retrieve the selected model data and to make the REST call successful. We have done it specifying the parameter map as described below.

var dataSourceobj = new kendo.data.DataSource({
 transport: {
     read:{
         url:RestBaseURL + "Cash/GetAccountInfo?uniqueaccessid=" + uniqueaccessid + "",
         dataType: "json",
          type: "POST"
           },
 
     destroy:{
         url: RestBaseURL + "Account/DeleteAccount?uniqueaccessid=" + uniqueaccessid ,
         dataType: "json",
         type: "POST",
         async: false,
         contentType: "application/json; charset=utf-8",
         cache: false
          },
     parameterMap: function(options, type) {
     if (type == "destroy")
     {
      
     return JSON.stringify( {
     'uniqueaccessid': uniqueaccessid,
     'accountId': options.models[0].id.toString()
                            });
     }
                                           }
 
 },        
 batch: true,
 pageSize: 10,
 schema: {
     type: "json",
     model: {
         fields: {
             id : {type: "number"},
             lender: { type: "string" },
             accountnumber: { type: "string" },
             rate: { type: "number" },
             startingbalance: { type: "number" },
             emergencyfund: { type: "number" },
             startingdate: { type: "date" },
             balance: { type: "number" }
                 }
             }
         }
  });


Regards
Tags
Grid
Asked by
Millennium
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Millennium
Top achievements
Rank 1
Share this question
or