Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 是否可以使用角度';s$http服务?_Angularjs_Angularjs Http - Fatal编程技术网

Angularjs 是否可以使用角度';s$http服务?

Angularjs 是否可以使用角度';s$http服务?,angularjs,angularjs-http,Angularjs,Angularjs Http,在我的Angular应用程序中,我有一个利用$http从服务器检索数据的服务。服务器端点使用HMAC身份验证,并希望查询字符串参数在URL上按特定顺序排列 ,因此似乎无法指定自定义参数顺序 下面是一个例子: this.apiCall = function() { return $http({ method: 'GET', url: 'http://example.com/url/v1/endpoint', params: {

在我的Angular应用程序中,我有一个利用
$http
从服务器检索数据的服务。服务器端点使用HMAC身份验证,并希望查询字符串参数在URL上按特定顺序排列

,因此似乎无法指定自定义参数顺序

下面是一个例子:

this.apiCall = function() {
    return $http({
        method: 'GET',
        url: 'http://example.com/url/v1/endpoint',
        params: {
            'c': 'cdata',
            'a': 'adata',
            'b': 'bdata'
        }
    });
};
Angular将以
http://example.com/url/v1/endpoint?a=adata&b=bdata&c=cdata
,但我需要保留指定参数的顺序,
http://example.com/url/v1/endpoint?c=cdata&a=adata&b=bdata

我意识到我可以手动将参数添加到URL字符串中,但这不是很友好,而且它不允许在
$http
拦截器中轻松管理

Angular可能会对参数进行排序,以在浏览器实现中保持一致的行为,因为ECMAScript中未指定对象顺序


不管怎样,是否有人知道如何绕过排序参数的默认角度行为,以便构建一个保留指定参数的URL?

我最终创建了一个基本的拦截器,以保持“指定”参数的顺序。如果在
$http
调用上设置了
keepParamsOrder
配置变量,则此拦截器将运行

在模块配置中:

$httpProvider.interceptors.push(function() {
    return {
        'request': function(config) {
            if (!config.keepParamsOrder || !config.params) {
                return config;
            }

            var queryStrings = [];
            for (var key in config.params) {
                if (config.params.hasOwnProperty(key)) {
                    queryStrings.push(key + '=' + config.params[key]);
                }
            }

            // Reset the params to be empty
            config.params = {};

            config.url += (config.url.indexOf('?') === -1) ? '?' : '&';
            config.url += queryStrings.join('&');

            return config;
        },
    };
});
告诉它在服务调用配置中运行:

this.apiCall = function() {
    return $http({
        method: 'GET',
        url: 'http://example.com/url/v1/endpoint',
        params: {
            'c': 'cdata',
            'a': 'adata',
            'b': 'bdata'
        },
        keepParamsOrder: true
    });
};

我对您的解决方案进行了改进,以创建更加流线型且保证有效的解决方案:

$httpProvider.interceptors.push(function() {
    return {
        request: function (config) {
            if (!config.paramOrder) {
                return config;
            }

            // avoid leaking config modifications
            config = angular.copy(config, {});

            var orderedParams = [];
            config.paramOrder.forEach(function (key) {
                if (config.params.hasOwnProperty(key)) {
                    orderedParams.push(encodeURIComponent(key) + '=' + encodeURIComponent(config.params[key]));
                    // leave only the unordered params in the `params` config
                    delete config.params[key];
                }
            });

            config.url += (config.url.indexOf('?') === -1) ? '?' : '&';
            config.url += orderedParams.join('&');

            return config;
        },
    };
});
使用以下命令调用:

$http({
    method: 'GET',
    url: 'http://example.com/url/v1/endpoint',
    params: {
        a: 'aValue',
        b: 'bValue',
        c: 'cValue'
    },
    paramOrder: ['c', 'a']
});

获取以键
c
开头,后跟
a
的查询字符串。
paramOrder
中未提及的参数将附加在有序参数之后(按字母顺序)。

在我看来,您已经回答了您的问题。查看链接的源代码,很明显,强制执行参数顺序的唯一方法是自己构造查询字符串并将其作为URL的一部分提供。遇到一些特殊的
参数
语法时,您可以为
$http
转换配置创建一个装饰器。@hon2a是的,我最终创建了一个装饰器来自定义构建URL。您的解决方案不知怎的缺乏,因为您仍然在对象中传递
参数
。您可以识别一种特殊的
params
语法(涉及
Array
),或者添加
paramOrder
配置,其中包含您想要排序的
params
的名称,而不是添加
keepParamsOrder
。这很好,我喜欢。