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

Configure Data Transport to include Header / ContentType

1 Answer 242 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Veteran
Richard asked on 03 Mar 2021, 06:15 PM

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");
                        }
                    }
},

 

1 Answer, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 08 Mar 2021, 09:15 AM

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/.

Tags
Grid
Asked by
Richard
Top achievements
Rank 1
Veteran
Answers by
Georgi
Telerik team
Share this question
or