Configure Data Transport to include Header / ContentType

2 posts, 0 answers
  1. Richard
    Richard avatar
    33 posts
    Member since:
    Sep 2011

    Posted 03 Mar Link to this post

    Using a Kendo Grid to do inline editing.  It works absolutely great and love everything Kendo brings to the table.  I'm working on doing some refactoring and trying to remove as much duplicate code from an application because ... well because it needs to happen.  :-)

    So I'm curious, is there a way to specify default values for transport.  For example, below you can see a Read, Update, Destroy and in each case I have to set the content type, send auth tokens in the header, etc.

    Thanks,

    Richard

    transport: {
                        // eslint-disable-next-line consistent-return
                        parameterMap: function (data, type) {
                            if (type !== "read" && data) {
                                return kendo.stringify(data);
                            }
                        },
                        read: {
                            url: "https://api.domain.com/Billings/",
                            dataType: "json",
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("TOKEN", "SomethingHere");
                                xhr.setRequestHeader("SECRET", "SomethingHere");
                            }
                        },
                        update: {
                            type: "PUT",
                            url: "https://api.domain.com/Billing",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("TOKEN", "SomethingHere");
                                xhr.setRequestHeader("SECRET", "SomethingHere");
                            }
                        },
                        destroy: {
                            type: "DELETE",
                            url: "https://api.domain.com/Billing",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("TOKEN", "SomethingHere");
                                xhr.setRequestHeader("SECRET", "SomethingHere");
                            }
                        }
    },

     

  2. Georgi
    Admin
    Georgi avatar
    703 posts

    Posted 08 Mar Link to this post

    Hi Richard,

    You could define a base transport object and extend it within the grid's configuration.

    e.g.

    // define base transport config
    
                var baseTransport = {
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("TOKEN", "SomethingHere");
                        xhr.setRequestHeader("SECRET", "SomethingHere");
                    }
                }
    
    // use it as follows
    
                        update: $.extend({
                            type: "PUT",
                            url: "https://api.domain.com/Billing",
                        }, baseTransport),
    

    I hope this helps.

    Regards,
    Georgi
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Back to Top