简体   繁体   English

在AngularJS应用中使用$ http从发布请求中删除标头字段

[英]Strip header fields from a post request using $http in an AngularJS app

In an AngularJS app I'm posting a form to a payment gateway. 在AngularJS应用中,我正在将表单发布到付款网关。 I have a header field called "Authorization" to pass my session token to the backend API. 我有一个名为“ Authorization”的标头字段,用于将我的会话令牌传递给后端API。

When trying to make a request to the payment gateway I get the following error: 尝试向支付网关提出请求时,出现以下错误:

XMLHttpRequest cannot load https://transact.nab.com.au/test/directpostv2/authorise . XMLHttpRequest无法加载https://transact.nab.com.au/test/directpostv2/authorise Request header field Authorization is not allowed by Access-Control-Allow-Headers. 请求标头字段Access-Control-Allow-Headers不允许授权。

When making the same request using Postman everything works fine, with a 200 status code. 使用Postman发出相同请求时,一切正常,状态代码为200。

I've created a plunkr for a quick demo which also gets a 200 status http://plnkr.co/edit/8Ts6s0VDTTUVVC9dbCJC 我为快速演示创建了一个plunkr,它也具有200个状态http://plnkr.co/edit/8Ts6s0VDTTUVVC9dbCJC

$http({
    method: 'POST',
    url: authoriseURL,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: nabPaymentData
}).
success(function (response, status, headers) {
    alert('Success:' + response + ' status:' + status + ' headers:' + headers);
}).
error(function (err, status, headers) {
    alert('Error:' + err + ' status:' + status + ' headers:' + headers);
});

Is there a way to strip header fields for one single post request? 有没有一种方法可以剥离单个帖子请求的标头字段?

As my comment above you can use interceptor to remove auth header only for request to payment gateway. 正如我上面的评论,您可以使用拦截器删除仅用于向支付网关请求的身份验证标头。 The solution will be like this. 解决方案将是这样。 First we define interceptor using factory. 首先,我们使用工厂定义拦截器。 Later we push the interceptor in angular config. 稍后,我们将拦截器按角度配置推送。

app.factory('myInterceptor', ['$log', function($log) {
    return {
    // optional method
    'request': function(config) {
      $log.debug(config.url);
      // if request to payment gateway, delete the auth header
      if(config.url.indexOf('https://transact.nab.com.au') > -1) {
        $log.debug('before deleting auth header');
        $log.debug(config.headers);
        delete config.headers.Authorization;
        $log.debug('after deleting auth header');
        $log.debug(config.headers);
      }
      $log.debug(config.url);
      $log.debug(config.headers);
      // do something on success
      return config;
    },


  };
}]);

app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('myInterceptor');
}]);

You can remove the Authorization field for just that POST request by setting it to undefined : 您可以通过将其设置为undefined来仅删除该POST请求的Authorization字段:

$http({
  method: 'POST',
  url: authoriseURL,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': undefined
  },
  data: nabPaymentData
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM