gpt4 book ai didi

angularjs - 防止 AngularJS 路由中的 url 编码

转载 作者:行者123 更新时间:2023-12-03 06:25:41 25 4
gpt4 key购买 nike

目前,当我将查询字符串传递到 $location 的 search() 方法时,我的查询字符串是 uri 编码的

示例

$location.path('/some_path').search({'ids[]': 1})

变成了

http://some_url/some_path?ids%5B%5D=1

我想知道是否有办法解决这个问题?

最佳答案

问题在于 .search() 使用的encodeUriQuery 内部使用encodeURIComponent 和 this function转义除以下字符之外的所有字符:字母、十进制数字、 - _ 。 ! ~ * ' ( )

Angular 内部的当前函数 source code :

/**
* 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, ']').

关于angularjs - 防止 AngularJS 路由中的 url 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21473796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com