2014-01-31 71 views
9

目前,当我通过我的查询字符串为$位置的搜索()方法,我的查询字符串的URI编码防止URL编码

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

成为

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

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

+0

我想有帕拉姆名称的语义定义,而不是使用类似逗号分隔列表的名称。 –

+1

有一种解决方法,但更重要的问题是,如果你?特殊字符应在URL中编码。如果你不想显示它是'丑'使用一个职位,而不是一个get。 –

+2

这是什么原因?即使你拿出一个好的,也有一百万人反对。无论AngularJS如何对URL进行编码,当调用$ location.search()时,它都会将URL解码为原始对象。在这一点上,做你想做的事情的唯一原因是让浏览器中的URL看起来更漂亮。 – Mike

回答

3

问题是.search()使用encodeUriQuery,它在内部使用encodeURIComponent,而this function转义除下列字符以外的所有字符:字母,十进制数字, - _。 ! 〜*'()

角的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, ']').