posting json creates urencoded string results in 400 bad request

7 posts, 1 answers
  1. Jonas
    Jonas avatar
    33 posts
    Member since:
    Sep 2009

    Posted 23 Jan 2012 Link to this post

    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
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 23 Jan 2012 Link to this post

    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!
  3. Kendo UI is VS 2017 Ready
  4. Jonas
    Jonas avatar
    33 posts
    Member since:
    Sep 2009

    Posted 23 Jan 2012 Link to this post

    sorry, same result.
  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 23 Jan 2012 Link to this post

    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!
  6. Jonas
    Jonas avatar
    33 posts
    Member since:
    Sep 2009

    Posted 23 Jan 2012 Link to this post

    How do you mean? If I change like that it does not post any data.
  7. Answer
    Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 23 Jan 2012 Link to this post

    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!
  8. Jonas
    Jonas avatar
    33 posts
    Member since:
    Sep 2009

    Posted 23 Jan 2012 Link to this post

    Ah, that was it, thanks Atanas. I've been struggling much  with this. Awesome with your quick response. Continuing to evaluate the grid.
Back to Top
Kendo UI is VS 2017 Ready