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

Cannot add a custom HTTP header

4 Answers 539 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Asat
Top achievements
Rank 1
Asat asked on 29 Nov 2013, 12:45 PM
Hi, 

I just started working with KendoUI mobile and was trying to call a REST Service. Below is my code,

  <script>
                $(document).ready(function() {
                    // create a template using the above definition
                    var template = kendo.template($("#template").html());

                    var dataSource = new kendo.data.DataSource({
                        type: "odata",
                        type: 'POST',
                        transport: {
                            read: "https://172.17.4.172:8443/prt/rest/crmapp/api/CrmGetAccounts",
                            beforeSend: function(xhr)
                                  {
                                          xhr.setRequestHeader('X-token','711761306837443264')
                                  }
                        },
                        change: function() { // to the CHANGE event of the data source
                            // update the max attribute of the "page" input
                            $("#page").attr("max", this.totalPages());

                            $("#products").html(kendo.render(template, this.view()));
                        }
                    });

                    // read data from the remote service
                    dataSource.read();
                });
            </script>


Asyou can see I am trying it our with changing sample code. My issue is the X-token is not getting added to the request header. I want it ot be passed as w HTTP header for the service to work.

Any help would be really appreciated,


Thanks,

Asat

4 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 02 Dec 2013, 08:52 AM
Hello Asat,

There is a syntax mistake in configuration properties nesting. Please try the following:
transport: {
    read: {
        url: "https://172.17.4.172:8443/prt/rest/crmapp/api/CrmGetAccounts",
        beforeSend: function(xhr) {
            xhr.setRequestHeader('X-token','711761306837443264')
        }
    }
}


Regards,
Alexander Valchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Sergiu
Top achievements
Rank 1
answered on 03 Dec 2014, 02:27 PM
This is for only one datasource, but I have like more than 100 datasources in my application. Is it possible to create a generic setting so that I can add header parameters for every datasource in only one place? Like a global setting.
0
Sergiu
Top achievements
Rank 1
answered on 04 Dec 2014, 11:18 AM
I have found a solution for everyone who is interested:
(function ($, kendo) {
    "use strict";
    kendo.myappns = kendo.myappns || {};
    kendo.myappns.DataSource = kendo.data.DataSource.extend({
        init: function (options) {
            if (options.transport && options.transport.read) {
                options.transport.read.beforeSend = function (xhr) {
                    xhr.setRequestHeader('Auth-Token', mytoken);
                };
 
            }
            kendo.data.DataSource.fn.init.call(this, options);
        }
    });
})($, kendo);
and then I just replace kendo.data.DataSource with kendo.myappns.DataSource
0
David
Top achievements
Rank 1
answered on 15 Sep 2015, 07:13 AM

 Hi all,

 I don't know whats happening on the below ajax calling. i tried lot but no use

*header*  not work. Please suggest me on the below code.

Note : the below request always goes with "GET" type and without header "token".......

       var url = 'api_url';
        var dataSource = new kendo.data.DataSource({
            type: "odata",
            transport: {
                read: {
                    url: url + 'GetCompanyContacts?UserId=123&CompanyId=123',
                    type:"POST",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('TOKEN', 'tekenstring');
                    }
                }

            },
            schema: {
                total: "d.__count"
            },
            sort: {
                field: "SubCategoryCode",
                dir: "desc"
            },
            serverPaging: true,
            serverSorting: true,
            pageSize: 50
        });
thanks in advance 

 

 

Tags
Data Source
Asked by
Asat
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Sergiu
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or