AngularJS:为外部URL post请求设置http头

AngularJS:为外部URL post请求设置http头,angularjs,Angularjs,要求是将数据从Angular Controller发布到外部URL。在尝试了各种方法之后,我发现下面博客中建议的方法是有效的 问题是我需要在HTTP头中发送几个自定义参数。而我却无法实现同样的目标。我甚至试过拦截器,但他们都没用。我甚至尝试在指令中使用控制器,但也不起作用 HTML代码 角度控制器 首先尝试使用简单的$http和文档中的标题,然后使用更高级的smth感谢usggestion Petr。我尝试过简单的$http,但没有成功 <div class="row continue_l

要求是将数据从Angular Controller发布到外部URL。在尝试了各种方法之后,我发现下面博客中建议的方法是有效的

问题是我需要在HTTP头中发送几个自定义参数。而我却无法实现同样的目标。我甚至试过拦截器,但他们都没用。我甚至尝试在指令中使用控制器,但也不起作用

HTML代码

角度控制器


首先尝试使用简单的$http和文档中的标题,然后使用更高级的smth感谢usggestion Petr。我尝试过简单的$http,但没有成功
<div class="row continue_login append_bottom10">
            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-5 fullWidth append_bottom8">
                <input type="submit" class="btn" name="submit" value="Test HTTP POST" ng-click="proceedWithCheckout()"/>
                <input type="reset" class="btn" name="reset" value="Cancel"/>
            </div>
        </div>
        <div auto-submit-form event="gateway.redirect"></div>
kmphApp.controller("PGCtrl", function ($scope, $http, $location, $rootScope, $sce) {

    $scope.proceedWithCheckout = function () {

        var signature;
        $http.post('/genRequestHMAC',
            {username: $scope.kmph_user, password: $scope.kmph_pwd})
            .success(function (data, status, headers, config) {

                var hmacData = data;
                if (hmacData != undefined) {
                    signature = hmacData.signature;
                    alert('inside this' + signature);


                    var data = {
                        redirectUrl: 'https://sandbox.mypaymentgateway.com',
                        redirectMethod: 'POST',
                        redirectData: {
                            'input1': 'value1',
                            'input2': 'value2'
                        }
                    }
                    data.redirectUrl = $sce.trustAsResourceUrl(data.redirectUrl);


                    $rootScope.$broadcast('gateway.redirect', data);
                } else {
                    //$location.path("/");
                }
            })
            .error(function (data, status, headers, config) {
                alert('failure');
            })
    }
});

kmphApp.directive('autoSubmitForm', ['$timeout', function ($timeout) {
    return {
        replace: true,
        scope: {},
        template: '<form action="{{formData.redirectUrl}}" method="{{formData.redirectMethod}}">' +
        '<div ng-repeat="(key,val) in formData.redirectData">' +
        '<input type="hidden" name="{{key}}" value="{{val}}" />' +
        '</div>' +
        '</form>',
        controller: function($scope, $element, $attrs, $http) {
            $http.defaults.headers.common["X-Vik"]='123';
            console.log('$http='+$http.defaults.headers.common["X-Vik"]);
        },
        link: function ($scope, element, $attrs) {
            $scope.$on($attrs['event'], function (event, data) {


                $scope.formData = data;
                console.log('redirecting now!');
                $timeout(function () {
                    element.submit();
                })
            })
        }
    };
}]);