I have a serious issue with odata requests and an authentication header I have to set - it just is not working! Here is the transport part of my datasource definition (done inside the grid configuration:
transport: {
type:
"odata"
,
read: {
url:
"http://........"
,
beforeSend:
function
(xhr) {
var
auth =
'Bearer '
+ token;
xhr.setRequestHeader(
'Authorization'
, auth);
}
},
},
If I remove the line saying "type = odata", then the header is correctly set (of course, the request is not delivering useful data then...). If I add the line, the "beforeSend" part is executed, but the header is not set in the request.
Could this be a bug?
Best regards,
Gheorghe
7 Answers, 1 is accepted
As you may know when odata type is set, by default the request will be executed via JSONP. Therefore, due to the way JSONP works there is not way to set the authorization headers. However, if the service you are trying to access is in the same domain as the application or the CORS are enabled you should set the read request dataType option to json, which will then allow setting the authorization headers.
transport: {
type:
"odata"
,
read: {
url:
"http://........"
,
dataType:
"json"
,
beforeSend:
function
(xhr) {
var
auth =
'Bearer '
+ token;
xhr.setRequestHeader(
'Authorization'
, auth);
}
},
},
Regards,
Rosen
Telerik
I have exactly the same problem. My DataSource is initialized like this:
kendo.data.DataSource({
type: "odata-v4",
transport: {
read: {
url: dataUrl,
dataType: 'json',
beforeSend: function (req) {
var auth = self.options.authenticationType + " " + self.options.authenticationToken;
req.setRequestHeader('Authorization', auth);
}
}
}
});
The authorization header is not set however. Is this due to odata-v4?
The code you have pasted should assign the authorization headers as expected. Please provide a small runnable sample in which the issue can be observed locally.
Regards,
Rosen
Telerik
I also have the same issue, my request headers are not being set.
type:
"odata"
,
transport: {
read: {
url:
"https://...."
,
dataType:
'json'
,
beforeSend:
function
(req) {
req.setRequestHeader(
'Authorization'
, auth);
req.setRequestHeader(
'Access-Control-Allow-Origin'
,
'*'
)
}
},
Hello Bill,
Maybe you could provide a small runnable sample which demonstrate the issue you are experiencing. Also you may test your service and AJAX request configuration by making the same request but using just jQuery.ajax. As you know, the DataSource remote transport uses jQuery.ajax to make AJAX requests, thus same configuration should work for both.
Regards,
Rosen
Telerik
Hello Jason,
As mentioned in the initial message using the beforeSend is the way to modify the request header. I have mentioned that the DataSource is not directly involved in send the headers as the configuration is passed to the underlying jQuery ajax call behind the scenes. Thus, the same approach applicable with jQuery (and discussed here for example) can be applied to the DataSource too. This is why I have suggested that in order to locally troubleshoot the problem a jQuery ajax call can be used leaving aside the DataSource. Then the same configuration can be used with the DataSource. If it cannot be get to work with jQuery ajax, then the problem may be somewhere else, for example the configuration of the server. This is the steps to self troubleshoot the issue, otherwise a sample project (leaving the server-side of the things out as much as possible as it is not directly related to Kendo UI) which we can look at locally will be required.
Regards,Rosen
Telerik