1

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?

Scott
  • 2,456
  • 3
  • 32
  • 54

1 Answers1

1

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


In a POST, data that is appended in the header in the Form Data. You can see this in this failed ajax call with a network sniffer.
$.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


I suggest you instead change your controller to except a post message and just have that data embedded in the Form Data (like you are doing now)

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));
}
Community
  • 1
  • 1