创建自定义标头并传递凭据?

时间:2014-02-21 15:52:41

标签: angularjs

我是angular.js的新手,我正在尝试为请求添加自定义标头。

我有一个功能,通过它我必须从服务

获得响应
function getInsuredWebAccount(insuredId) {

        };
        var deferred = $q.defer();
        var related = $resource(webAccountServiceHostName + '/user/v1/account/:insuredId');
        related.get({ insuredId: insuredId }, function onSuccess(response) {

            var webInfo = response.results;
            deferred.resolve(response);

        }, function onFailure(response) {
            deferred.reject(response);
        });

        return deferred.promise;
    }

我必须在标题中传递("Authorization", "Bearer c3J1bml0ZTo1OUJFODUwNUIyRjM0OEVGQTI1RTU1RjU4NEVGNUE0")

有人能引导我朝正确的方向前进吗?

1 个答案:

答案 0 :(得分:0)

您可以在资源构造函数中设置标题:

var related = $resource(webAccountServiceHostName + '/user/v1/account/:insuredId', {}, {
    get: {
        headers: {
            Authorization: "Bearer c3J1bml0ZTo1OUJFODUwNUIyRjM0OEVGQTI1RTU1RjU4NEVGNUE0"
        }
    }
});