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

posting json creates urencoded string results in 400 bad request

6 Answers 358 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonas
Top achievements
Rank 1
Jonas asked on 23 Jan 2012, 10:07 AM
Hi,
I'm trying to post pure json data this way:
                                create: {
                                    url: crudServiceBaseUrl,
                                    dataType: "jsonp",
                                    contentType: "application/json",
                                    type: "POST"
                                },
                                parameterMap: function (options, operation) {
                                    if (operation !== "read" && options) {
                                        return { models: kendo.stringify(options) };
                                    }
                                }
                          }
                          batch:false

(I use 'options' instead of 'options.models' since I'm not using batch post)

but the post is not formatted correctly. If I look at the request payload (in chrome dev tools) it says
models=%7B%22Id%22%3Anull%2C%22Content%22%3A%22somecontent%22%2C%22Order%22%3A0%2C%22Done%22%3Afalse%7D 

which the server does not like and responds with 400 bad request

however, if I add my own jQuery.ajax function like this:

                    $.ajax({
                        type: 'POST',
                        url: '/api/todos',
                        data: kendo.stringify(options),
                        contentType: 'application/json',
                        dataType: 'jsonp',
                        success: function (data) {
                            _resData = data;
                        },
                        async: false
                    });                    

the request is ok:
{"Id":null,"Content":"somecontent","Order":0,"Done":false} 

and the server responds OK

What am I doing wrong? 
Thanks

6 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 23 Jan 2012, 10:12 AM
Hi,

 The problem is that you are using "jsonp" data type. Try with "json":

                             create: {
                                    url: crudServiceBaseUrl,
                                    dataType: "json",
                                    contentType: "application/json",
                                    type: "POST"
                                }, 


All the best,
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
Jonas
Top achievements
Rank 1
answered on 23 Jan 2012, 10:18 AM
sorry, same result.
0
Atanas Korchev
Telerik team
answered on 23 Jan 2012, 10:31 AM
Hi,

The parameterMap is not implemented as the $.ajax data function? Have you tried this:

if (operation !== "read" && options) {
      kendo.stringify({ models: options});


You still need dataType:"json" though.

All the best,
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
Jonas
Top achievements
Rank 1
answered on 23 Jan 2012, 10:59 AM
How do you mean? If I change like that it does not post any data.
0
Accepted
Atanas Korchev
Telerik team
answered on 23 Jan 2012, 11:04 AM
Hi,

 Sorry, I forgot a return. The right code should be:

if (operation !== "read" && options) {
      return kendo.stringify({ models: options});
}  

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
Jonas
Top achievements
Rank 1
answered on 23 Jan 2012, 11:19 AM
Ah, that was it, thanks Atanas. I've been struggling much  with this. Awesome with your quick response. Continuing to evaluate the grid.
Tags
Grid
Asked by
Jonas
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Jonas
Top achievements
Rank 1
Share this question
or