在微风设置AJAX头微风、AJAX

2023-09-10 19:15:08 作者:咸咸的海风

如何设置让清风前的头提出要求?

     

例如:我的服务需要一定的关键是在请求的一部分   一个头名的X服务的关键。截至目前,我正在使用jQuery Ajax和   放大,所以pretty的易于设置的标题。因为我没有任何   在这清风发出请求,我​​怎么通过额外的控制   东西像头?

这个问题是我们IdeaBlade论坛上发表sujesharukil。我重新发布的问题和答案在这里,因为我认为这将是有益的微风堆栈溢出社区。 解决方案

随着微风0.70.1,我们现在完全自定义或更换微风客户端和服务器上的Web服务之间的任何阿贾克斯通信能力支持。

在我们的Ajax支持的微风文档仍在进行中,但幸运地是,将让你开始。

要控制在每一个Ajax请求的海风让你可以执行以下code,当你的应用程序第一次启动时的标题。

  VAR ajaxImpl = breeze.config.getAdapterInstance(AJAX);
 ajaxImpl.defaultSettings = {
       标题:{
           //任何CORS或要指定其他头。
           X试验头:foo2的
       },
};
 
JavaScript之Ajax

另外,您也可以拦截Ajax调用个人并添加标题选择基于该请求。

  VAR ajaxImpl = breeze.config.getAdapterInstance(AJAX);
 ajaxImpl.defaultSettings = {
       beforeSend:功能(jqXHR,设置){
              //检查jqXHR或设置,并相应地自定义标题。
              jqXHR.setRequestHeader(X试验之前,发送标头,foo2的);
       }
 };
 

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.

解决方案

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

 
精彩推荐
图片推荐