我有一个声明如下的角度资源
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中的其他内容。有什么想法吗?