【问题标题】:Is it possible to specify parameter order with Angular's $http service?是否可以使用 Angular 的 $http 服务指定参数顺序?
【发布时间】:2015-01-23 17:00:11
【问题描述】:

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

Angular sorts the $http parameters when it builds the URL,因此似乎无法指定自定义参数顺序。

这是一个例子:

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

Angular 会将 URL 构建为 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 中没有指定对象排序。

无论如何,有谁知道如何解决 Angular 对参数进行排序的默认行为,以构建一个保留指定参数的 URL?

【问题讨论】:

  • 在我看来,您已经回答了您的问题。查看链接的源代码,很明显,强制参数顺序的唯一方法是自己构造查询字符串并将其作为 URL 的一部分提供。您可以在遇到一些特殊的params 语法时为$http 转换配置创建一个装饰器。
  • @hon2a 是的,我最终创建了一个装饰器来自定义构建 URL。

标签: angularjs angularjs-http


【解决方案1】:

我最终创建了一个基本的拦截器来保持“指定的”参数顺序。如果在 $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
    });
};

【讨论】:

  • 您的解决方案在某种程度上缺乏,因为您仍在一个对象中传递params,这是一个unordered collection。除了添加keepParamsOrder,您还可以识别特殊的params 语法(涉及Array)或添加包含您想要订购的params 名称的paramOrder 配置。
【解决方案2】:

我改进了您的解决方案,以创建更精简并保证工作的东西:

$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 中未提及的参数将附加在有序参数之后(按字母顺序)。

【讨论】:

  • 好电话,我喜欢这个。
猜你喜欢
  • 2023-01-29
  • 1970-01-01
  • 2015-09-18
  • 2015-10-15
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多