1
votes

I have a grid from which I want to create a new item. It looks like this:

self.PermissionTypeGrid = kendo.observable({
        isVisible: true,
        permissionTypes: new kendo.data.DataSource({
            schema: {
                model: { id: "PermissionType" },

            transport: {
                read: {
                    url: "/api/ServiceApi?method=Ref/SystemPermissionTypes",
                    type: "GET"
                },
                create: {
                    url: "/api/ServiceApi?method=Ref/SystemPermissionType",
                    type: "POST"
                },
                parameterMap: function(data, type) {
                    if (type == "create") {
                        return { models: kendo.stringify(data) };
                    }
                }
            }
        })
    });
    kendo.bind($("#permissionTypeGrid"), self.PermissionTypeGrid);

The parameterMap section returns a string that looks like this:

{"PermissionType":"test","Description":"test"}

I need to create a url that looks like this: "/api/ServiceApi?method=Ref/SystemPermissionType&data={"PermissionType":"test","Description":"test"}"

In other words, I have the correct stringified data. How do I get it to be appended to the url I specify?

1

1 Answers

1
votes

Set your type to GET. HOWEVER. This is bad practice.


$.ajax({
  url:'http://test.com',
  data:{'test':'test'},
  type:'POST'
});

If you look at the network traffic you will see that the data is not a part of the url.

Now if you were to try this as a GET instead

$.ajax({
  url:'http://test.com',
  data:{'test':'test'},
  type:'GET'
});

You will see that instead the data is appended to the query string of the URL

http://test.com/?test=test 

Beware there is a limit to the length of a URL, and just submitting data via a GET method can fail if you try to append to much data. Also you can have your server treat GETs and POSTs differently.

You can search Stack Overflow for more expanded explanation of what each should be used for.
Like this: GET vs POST in Ajax


Here is an example from kendo CRUD opertaions of a create controller method http://demos.telerik.com/aspnet-mvc/grid/editing-inline

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingInline_Create([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
{
  if (product != null && ModelState.IsValid)
  {         
    productService.Create(product);                                 
  }
  return Json(new [] { product }.ToDataSourceResult(request, ModelState));
}