Setting AJAX headers in Breeze

倖福魔咒の 提交于 2019-12-10 13:53:30

问题


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.


回答1:


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");
       }
 };


来源:https://stackoverflow.com/questions/13616445/setting-ajax-headers-in-breeze

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!