Breeze - Adding headers to request

后端 未结 1 389
野性不改
野性不改 2021-01-25 22:11

I am integrating with a server using Breeze as OData client, I made the login using regular jQuery post request and got back a token which I need to add to every request, how ca

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-25 22:37

    A similar question appeared in the comments to Brian Noyes (wonderful) Pluralsight course on Breeze.

    The gist of it as follows.

    When you choose the OData dataservice, Breeze delegates communication with the server (the AJAX calls) to DataJS. That's how the Breeze OData dataservice is implemented. We think this is a wise choice because Microsoft (as I understand it) is substantially responsible for maintaining DataJS; better for Breeze to let them lead.

    So what you should be looking for is DataJS hooks. The fellow who posed the question, @bhlaban, wrote this:

    "I think I found a work around: Since breeze uses datajs under the hood for odata, I just did the following (from datajs forum) to set the authorization header:

    var oldClient = OData.defaultHttpClient;
    
    var myClient = {
         request: function (request, success, error) {
             request.headers.Authorization = $http.defaults.headers.common['Authorization'];
             return oldClient.request(request, success, error);
         }
    };
    
    OData.defaultHttpClient = myClient;
    

    If you're not using Angular's $http, you'll get your header in some other way.

    0 讨论(0)
提交回复
热议问题