URLEncode - getting it to work how I want
I want to URL encode email addresses. I input "bmackey@foo.com" on w3schools, but it did not encode "@开发者_如何学JAVA" or ".". I am using encodeURI()
and have the same results. What is going on?
At least, I assume it did not encode because I see in FireBug Net tab:
GET http://dev:8989/SJMUserManager/Service/Index/bmackey@foo.com
I expected to see
GET http://dev:8989/SJMUserManager/Service/Index/bmackey%40foo%2Ecom
@
and .
are not special characters in HTTP URIs.
The characters that need encoding include space (which becomes +
), +
itself, and %
, which is used to hex-encode.
Other characters outside the normal ASCII range of 32 - 127 and various other characters within that range use that %
hex encoding.
For correct handling, you should consider using encodeURIComponent()
but only on the part of the URI that was user supplied. If you encode the entire URI that way you'll get an invalid URI.
You can try using encodeURIComponent
instead.
encodeURIComponent('GET http://dev:8989/SJMUserManager/Service/Index/bmackey@foo.com')
Return:
"GET%20http%3A%2F%2Fdev%3A8989%2FSJMUserManager%2FService%2FIndex%2Fbmackey%40foo.com"
@ is a reserved character, and therefore not encoded, you can see the reference at: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI
精彩评论