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

Setting POST and ContentType for READ fails To Create JSON Payload

1 Answer 1431 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Nate
Top achievements
Rank 1
Nate asked on 23 Jan 2015, 11:37 PM
Hello -

This is an offshoot of: http://www.telerik.com/forums/problem-with-datasource-post-request-for-read-operation

Unfortunately, that thread is closed so I can't follow-up.

However, it seems to me that there's a potential bug with DataSource (used inside a Kendo Grid) when doing a READ using POST and contentType = "application/json".

Here is my javascript:
dataSource: {
   transport: {
      read: {
           url: "rest/books",
           type: "POST",
           contentType: "application/json; charset=utf-8"
      }
...

However, based on examining the network traffic, here's the POST request information:
Remote Address:[::1]:8082
Request URL:rest/books
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:31
Content-Type:application/json; charset=UTF-8
Host:localhost:8082
Origin:http://localhost:8081
Pragma:no-cache
Referer:http://localhost:8081/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36
Request Payload
take=5&skip=0&page=1&pageSize=5

You can see in that very last line, the paging parameters for the Grid are being sent as key/value pairs (URL param encoding) instead of JSON.

I would think that since I'm setting contentType to JSON, and using a POST, that internally the DataSource would JSON encode my paging parameters.

The workaround (based on info in the link above) is to override the parameterMap function:
parameterMap: function (options) {
    return JSON.stringify(options);
}

This creates JSON in the Request Payload:
{"take":5,"skip":0,"page":1,"pageSize":5}

But it seems to me I shouldn't need to override parameterMap if I'm telling the datasource to use application/json.

My only guess is that Kendo is just passing the contentType to the internal AJAX call to set the POST header and not actually doing anything with that information (like converting the URL/form encoded parameters to JSON).

1 Answer, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 27 Jan 2015, 02:47 PM
Hi Nate,

As you may know the built-in remote transport uses jQuery.ajax to create AJAX request. Thus, setting the contentType will not affect how the data is serialized. As stated in the documentation you should use the parameterMap to serialize it to anything different than the format produced by $.params.

Regards,
Rosen
Telerik
 
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
Nate
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or