目前,当我将查询字符串传递到$location的search()方法中时,我的查询字符串被URI编码了。
示例:
$location.path('/some_path').search({'ids[]': 1})
变成
http://some_url/some_path?ids%5B%5D=1
我想知道是否有一种方法可以绕过这个问题?
目前,当我将查询字符串传递到$location的search()方法中时,我的查询字符串被URI编码了。
示例:
$location.path('/some_path').search({'ids[]': 1})
变成
http://some_url/some_path?ids%5B%5D=1
我想知道是否有一种方法可以绕过这个问题?
/**
* This method is intended for encoding *key* or *value* parts of query component. We need a custom
* method because encodeURIComponent is too aggressive and encodes stuff that doesn't have to be
* encoded per http://tools.ietf.org/html/rfc3986:
* query = *( pchar / "/" / "?" )
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* pct-encoded = "%" HEXDIG HEXDIG
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
function encodeUriQuery(val, pctEncodeSpaces) {
return encodeURIComponent(val).
replace(/%40/gi, '@').
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
}
replace(/%5B/gi, '[').
replace(/%5D/gi, ']').
$location.search()
时,它会将URL解码回到您的原始对象。在此时,您尝试做的唯一原因就是让浏览器中的URL看起来更漂亮。 - Mike Quinlan