【问题标题】:How to pass ampersand (&) to AngularJS resource parameter如何将 & 号 (&) 传递给 AngularJS 资源参数
【发布时间】:2013-09-06 19:13:53
【问题描述】:

我有一个角度资源声明如下

angular.module('xpto', ['ngResource'])
.factory('XPTO', function ($resource, $location) {
    var XPTO = $resource($location.protocol() + '://' + $location.host() +
    ':port' + '/myservice.svc/gerencia?sigla=:sigla',
        {
            port: ':' + $location.port()
        }
    );
    return XPTO;
})

我想调用服务传递一个包含与号 (&) 的参数,例如:

XPTO.query({ sigla: 'abc&d' }, function (gerencias) {
$scope.gerenciasBuscadas = gerencias;
});

但是,AngularJS 没有正确编码 &。它发送 "sigla=abc&d" 而不是 "sigla=abc%26d" ,导致我的服务器看到 "sigla" 的查询字符串参数值只是 "abc",而不是 "abc&d"。

查看 angular-resource-1.0.7.js,我看到以下内容:

/**
 * We need our custom method because encodeURIComponent is too aggressive and doesn't follow
 * http://www.ietf.org/rfc/rfc3986.txt with regards to the character set (pchar) allowed in path
 * segments:
 *    segment       = *pchar
 *    pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
 *    pct-encoded   = "%" HEXDIG HEXDIG
 *    unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
 *    sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
 *                     / "*" / "+" / "," / ";" / "="
 */
function encodeUriSegment(val) {
  return encodeUriQuery(val, true).
    replace(/%26/gi, '&').
    replace(/%3D/gi, '=').
    replace(/%2B/gi, '+');
}

因此,它会在将请求发送到服务器之前对“&”进行编码并解码。有没有什么地方可以在将 URL 发送到服务器之前对其进行自定义?更改 encodeUriSegment 是一种选择,但它可能会破坏 Angular 中的其他内容。有什么想法吗?

【问题讨论】:

    标签: javascript angularjs urlencode angularjs-resource


    【解决方案1】:

    这是 AngularJS 中的 bug。

    我发布了修复此问题的拉取请求。你可以帮我把它合并评论拉取请求本身here。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2018-09-13
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多