Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

How To Use Kendo UI Grid With Authentication

4.50/5 (2 votes)
29 Dec 2015CPOL 16.4K  
This tip is about how to tune up the Kendo Grid when using its authentication feature.

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

Introduction

When using Kendo Grid with authentication, you will need to tune up dataSource.transport correctly. For example:

JavaScript
dataSource: {
  type: "odata",
  transport: {
    read: {
      url: db.url + '/collections/' + this.query,
      beforeSend: function (request){
        request.setRequestHeader('Authorization', db.token);
      },
      dataType: "json"
    },
    create: {
      url: db.url + '/collections/' + this.query,
      headers: {
        Authorization: db.token,
        'content-type': 'application/json',
      }
    },
    update: {
      url: db.url + '/collections/' + this.query,
      headers: {
        Authorization: db.token,
        'content-type': 'application/json',
      }
    },
    destroy: {
      url: function(data) {
        return db.url + '/collections/allobjects' + "(" + data.id + ")";
      },
      headers: {
        Authorization: db.token,
        'content-type': 'application/json',
      }
    }
  },
  pageSize: 100,
  serverPaging: true,
  serverFiltering: true,
  serverSorting: true,
  schema: {
    errors: "error",
    model: {
      id: "id"
    }
  }
},

The most important thing is that header should contain:

JavaScript
Authorization: db.token

The example shows that it can be done two ways:

  1. Specify headers:
    JavaScript
    headers: {
      Authorization: db.token,
      'content-type': 'application/json',
    }
  2. Specify a function that should run before sending message to the server. The headers should be set within that function:
    JavaScript
    beforeSend: function (request){
      request.setRequestHeader('Authorization', db.token);
    },

Another important notice: Kendo Grid in OData mode uses jsonp by default for data reading. But jsonp request doesn’t allow headers changing. To be able to change headers, you need to set for read command:

JavaScript
dataType: "json"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)