Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting AJAX headers in Breeze

Tags:

ajax

breeze

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?

This question was posted by sujesharukil on our IdeaBlade forums. I am reposting the question and answer here since I think it will be useful to the Breeze Stack Overflow community.

like image 503
Ting Avatar asked Nov 28 '12 23:11

Ting


1 Answers

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.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.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");
       }
 };
like image 148
Ting Avatar answered Oct 13 '22 06:10

Ting