可以使用Angular的$ http服务指定参数顺序吗? [英] Is it possible to specify parameter order with Angular's $http service?

查看:105
本文介绍了可以使用Angular的$ http服务指定参数顺序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Angular app, I have a service that utilizes $http to retrieve data from a server. The server endpoint uses HMAC authentication and expects the query string parameters to be in a specific order on the URL.

角度对$http进行排序构建网址时输入参数,因此似乎无法指定自定义参数顺序.

Angular sorts the $http parameters when it builds the URL, so it doesn't seem possible to specify a custom parameter order.

这是一个例子:

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.

Angular will build the URL as http://example.com/url/v1/endpoint?a=adata&b=bdata&c=cdata, but I need to preserve the order of the params as specified, http://example.com/url/v1/endpoint?c=cdata&a=adata&b=bdata.

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

I realize I could just tack on the parameters to the URL string manually, but that is not very friendly and it doesn't allow for easy management in $http interceptors.

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

Angular probably sorts the parameters to keep a uniform behavior across browser implementations, as object ordering is not specified in ECMAScript.

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

推荐答案

我改进了您的解决方案,以创建更加流线化并保证能够正常工作的内容:

I improved upon your solution to create something more stream-lined and guaranteed to work:

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

to get a query string starting with key c followed by a. The params not mentioned in paramOrder will be appended after the ordered params (in alphabetical order).

这篇关于可以使用Angular的$ http服务指定参数顺序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆