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

remote WebApi not work

1 Answer 156 Views
Grid
This is a migrated thread and some comments may be shown as answers.
YJing
Top achievements
Rank 1
YJing asked on 05 Jan 2013, 01:12 PM
I read http://www.kendoui.com/code-library/mvc/grid/binding-to-a-web-apicontroller.aspx , but remote WebApi not work
@(Html.Kendo().Grid<KendoUIMvcApplication3.Models.Product>()
      .Name("Grid")
      .Columns(columns =>
      {
          columns.Bound(p => p.ProductName);
          columns.Bound(p => p.UnitsInStock);
          columns.Command(c =>
          {
              c.Edit();
              c.Destroy();
          });
      })
      .ToolBar(tools =>
      {
          tools.Create();
      })
      .Sortable()
      .Pageable()
      .Filterable()
      .DataSource(dataSource => dataSource
        .Ajax()
            .Model(model =>
            {
                model.Id(p => p.ProductID);
            })
            .Read(read => read.Url("http://192.168.1.10:699/api/Product").Type(HttpVerbs.Get))
            .Create(create => create.Url("http://192.168.1.10:699/api/Product").Type(HttpVerbs.Post))
            .Update(update => update.Url("http://192.168.1.10:699/api/Product").Type(HttpVerbs.Put))
            .Destroy(destroy => destroy.Url("http://192.168.1.10:699/api/Product").Type(HttpVerbs.Delete))
      )
)
 
<script>
 
$(function() {
    var grid = $("#Grid").data("kendoGrid");
 
    // WebAPI needs the ID of the entity to be part of the URL e.g. PUT /api/Product/80
    grid.dataSource.transport.options.update.url = function(data) {
        return "http://192.168.1.10:699/api/Product/" + data.ProductID;
    };
 
    // WebAPI needs the ID of the entity to be part of the URL e.g. DELETE /api/Product/80
    grid.dataSource.transport.options.destroy.url = function(data) {
        return "http://192.168.1.10:699/api/Product/" + data.ProductID;
    };
});
 
</script>


1 Answer, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 07 Jan 2013, 07:44 AM
Hello,

 This happens because of the same origin policy. Once cannot make ajax requests cross domain unless using JSONP or CORS. Currently WebAPI does not support JSONP or CORS out of the box however you can find some workarounds if you search online:
http://blog.bittercoder.com/2012/09/09/cors-and-webapi/

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!
Tags
Grid
Asked by
YJing
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or