Click here to Skip to main content
15,889,896 members
Articles / Programming Languages / Javascript
Tip/Trick

How To Use Kendo UI Grid With Authentication

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
29 Dec 2015CPOL 16.1K   1  
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)


Written By
CEO databoom
Russian Federation Russian Federation
This member doesn't quite have enough reputation to be able to display their biography and homepage.
This is a Organisation (No members)


Comments and Discussions

 
-- There are no messages in this forum --