PUT and DELETE http verbs don't work in transport, change "contentType" either

2 posts, 0 answers
  1. Rafael
    Rafael avatar
    8 posts
    Member since:
    Dec 2007

    Posted 14 Mar 2013 Link to this post

    Hello everybody:
    I'm currently testing KendoUI in order to adopt it for new developments and I have two big problems with the ajax request. You can download a complete solution from https://github.com/ragare62/AriUserManagement in order to test this behaviour. If you load AriUMSolution and start AriUMWebApi and AriUMWebHtml (UserGroupGrid.shtml) you'll access to a Kendo grid page and try to create, update or delete registers.

    PUT and DELETE http verbs don't work:
    When I specify the "type" attribute in "transport.create" to "POST", "transport.update" to "PUT" and "transport.destroy" to "DELETE" the result is always a request with the "OPTIONS" verb. Reading the JQuery documentation about ajax request I realize that "PUT" and "DELETE" could be problematic. Well, I said to myself I'll work only with "POST" so far, and then it came the second problem.

    When change "contentType" the request doesn't work:
    In my example when you create a new register a "POST" request is issued but webapi can't deserialize the JSON object in the request body. When I debugged that problem with Fiddler I realized that the Content-Type was "Content-Type: application/x-www-form-urlencoded; charset=UTF-8", if I changed "application/x-www-form-urlencoded"  to "application/json" with Fiddler everything worked fine. I tried to fix the content type in "transport.create" with the "contentType" attribute to "application/json". But if I did that, the http verb changes from "POST" to "OPTIONS".

    I tested these issues with "html" pages instead of "shtml" pages and with different browsers with the same result.
    Any help would be appreciate.
    Thanks in advance.

  2. Rosen
    Admin
    Rosen avatar
    3253 posts

    Posted 14 Mar 2013 Link to this post

    Hello Rafael,

    The behavior of PUT and DELETE verbs is caused by the usage of the CORS. In this case the browser does an OPTIONS request before PUT and DELETE. This is done because the browser wants to check the CORS policy before doing the real request. You may refer to this article for additional details.

    You may check if adding the following headers correct the issue:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, X-Requested-With, Origin, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
     
    }

    Regarding the deserialization issue. After fixing the the above issue and setting the correct contentType via the request options this should also be resolved.
    Regards,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top