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

How can I read custom Response Headers

7 Answers 552 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 26 Sep 2014, 02:40 PM
Hello!

The Rails service I connecting to, sends certain important data in the HTTP headers . I' can't figure out how to access that

Here is what I'm trying to do  - See schema {total } below in bold
01.productsDataSource = new kendo.data.DataSource({
02.  autoSync: true,
03.  pageSize: 10,
04.  serverPaging: true,
05.  serverSorting: true,
06.  serverFiltering: true,
07.  //serverGrouping: true,
08.  schema: {
09.    data: "data",
10.    total: "count", // THIS needs to be read from X-Total:31067  - line 9 in the code block below.
11.    model: {
12.      fields: {
13.        name:            {type: "string"},
14.        description:     {type: "string"},
15.        id:              {type: "number"}
16.      }
17.    }
18.  }
19.});

Response Headers
01.Server:nginx/1.6.0 + Phusion Passenger 4.0.42
02.Status:200 OK
03.X-Meta-Request-Version:0.3.4
04.X-Next-Page:2
05.X-Page:1
06.X-Powered-By:Phusion Passenger 4.0.42
07.X-Request-Id:1fd5e3b7541e7c0fee888b3799a26990
08.X-Runtime:0.238255
09.X-Total:31067
10.X-Total-Pages:1243
11.X-UA-Compatible:IE=Edge,chrome=1


Thank you!

7 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 30 Sep 2014, 07:31 AM
Hello Nick,

DataSource uses jQuery.ajax to request remote resource. That said you should be able to handle complete event of and inspect the headers available in the jqXHR object. Sample code snippet:
transport: {
 read: {
  url: "some url",
  complete: function(jqXHR, textStatus) {
   // inspect the content in the jqXHR object
   // jqXHR.getAllResponseHeaders() for example or related methods
  }
 }
}


Regards,
Nikolay Rusev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Bengt
Top achievements
Rank 1
answered on 10 Oct 2014, 09:23 AM
Hello Nikolay,

I'm having the same problem, need to get total-count from header response.
I tried hooking up the complete event as you recommended and I was able to extract the total count from the header data just fine.
The problem I run into is that it's fired after the schema.total function.
So I can't get the paging of the grid to work correct.

Regards
Bengt
0
Nick
Top achievements
Rank 1
answered on 10 Oct 2014, 06:46 PM
Hi Bengt. 
I don't know if it's an option for you but if you have any control over the api / backend  see if you can include that info in the jason rather than headers. It will make your life easier. I ran into the same problem you did, and since I was upgrading the server app from rails 3.2 to rails 4.2 I changed the api response to include meta info like pagination and some other stuff about the object.
0
Bengt
Top achievements
Rank 1
answered on 15 Oct 2014, 06:53 AM
Thanks for your reply Nick!
In this particular case I have control over the backend, but that is for sure not true all the time, and it requires to rewrite our web api due to "client" demands, not the way you would like it in a perfect world I believe.. :-)
0
Nikolay Rusev
Telerik team
answered on 16 Oct 2014, 12:37 PM
Hello,

Indeed data, total etc. field must be part of the response body. Otherwise the will no be picked from the DataSource.

Regards,
Nikolay Rusev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Nick
Top achievements
Rank 1
answered on 28 Oct 2014, 02:11 PM
@Nikolay Rusev

Hi. 

As bengt mentioned sometimes we have no control over api we consume, and it would helpful if there was a way to access the whole raw response for cases like this. Something along the lines of dataSource.raw() or dataSource.fullResponse() - this way Telerik doesn't need to account for all the edge cases, and the same time we would be able to accommodate api's we don't have control over.
0
Accepted
Petur Subev
Telerik team
answered on 30 Oct 2014, 09:37 AM
Hello Everyone,

In case you want to take full control over the logic that performs the XHR request you can define the read operation as function:

http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-transport.read

e.g.

http://dojo.telerik.com/@pesho/IQOKo

Kind Regards,
Petur Subev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
Nick
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Bengt
Top achievements
Rank 1
Nick
Top achievements
Rank 1
Petur Subev
Telerik team
Share this question
or