Print Page | Close Window

Setting headers with request

Printed From: IdeaBlade
Category: Breeze
Forum Name: Community Forum
Forum Discription: Build rich JavaScript apps using techniques you already know
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3764
Printed Date: 28-Mar-2024 at 5:44am


Topic: Setting headers with request
Posted By: sujesharukil
Subject: Setting headers with request
Date Posted: 02-Nov-2012 at 7:04pm
Another question. How do I set headers before letting breeze make a request?

Example: my service expects a certain key to be part of the request in a header name 'x-service-key'. Till now, I was using jquery ajax and amplify, so pretty easy to set up the header. Since I don't have any control over the request that breeze is making, how do I pass extra stuff like headers?

Thanks,
Suj



Replies:
Posted By: jtraband
Date Posted: 03-Nov-2012 at 12:52am
Great question!, and an oversight on our part.  We will be adding support for custom ajax headers in the next release, (or very soon thereafter).  I can give you a fix that involves hacking the source as a temporary fix if you need something sooner. 


Posted By: sujesharukil
Date Posted: 03-Nov-2012 at 9:42pm
I can wait for the next release. I am still evaluating breeze. Although I see Breeze.js as a powerful way to manage data and models on the client side, I still feel there are a few things missing before it can be included in an enterprise level library. Granted it is in Beta and it can do a lot and I know I will continue to watch this project and some day be in a position to actually make it our preferred library for the enterprise.

The decision finally came down to Complex type support. I know Breeze.js does not support complex types and it is in your roadmap and when it does, I Will be back again.

Keep the great work going. I will be using Breeze for almost anything that I build now. thank you.

-Suj


Posted By: jtraband
Date Posted: 13-Nov-2012 at 10:34am
As of Breeze 0.70.1 we now support for the ability to completely customize or replace any ajax communication between the breeze client and the web service on the server.

The Breeze documentation on our Ajax support is still in progress, but hopefully the following will get you started.

To control the headers on every ajax request that breeze makes you can execute the following code when your app first starts up.

     var ajaxImpl = breeze.core.config.getAdapterInstance("ajax");
     ajaxImpl.defaultSettings = {
           headers: {
               // any CORS or other headers that you want to specify.
               "X-Test-Header": "foo2"
           },
    };
  
Alternatively, you can intercept the individual ajax calls and add your headers selectively based on the request.

     var ajaxImpl = breeze.core.config.getAdapterInstance("ajax");
     ajaxImpl.defaultSettings = {
           beforeSend: function(jqXHR, settings) {
                  // examine the jqXHR or settings and customize the headers accordingly.
                  jqXHR.setRequestHeader("X-Test-Before-Send-Header", "foo2");
           }
     };


Posted By: sujesharukil
Date Posted: 15-Nov-2012 at 8:30pm
This works. Thank you. Keep em updates coming!



Print Page | Close Window