Using &url='+encodeURIComponent(url);
to pass a URL from browser to server will encode the url but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.
使用&url ='+ encodeURIComponent(url);将URL从浏览器传递到服务器将对url进行编码,但是当在服务器上对其进行解码时,url的参数将被解释为单独的参数,而不是单个url参数的一部分。
What is the recommended way to pass urls as url parameters ?
将url作为url参数传递的推荐方法是什么?
4 个解决方案
#1
48
encodeURIComponent()
should work. For example,
encodeURIComponent()应该工作。例如,
'&url=' + encodeURIComponent("http://a.com/?q=query&n=10")
produces
"&url=http%3A%2F%2Fa.com%2F%3Fq%3Dquery%26n%3D10"
(which doesn't have any &
or ?
in the value). When your server gets this url, it should be able to decode that to get the original:
(在值中没有任何&或?)。当您的服务器获取此URL时,它应该能够解码它以获取原始:
param["url"] = "http://a.com/?q=query&n=10"
I'm not sure what server you're using (e.g. Rails, Django, ...) but that should work "out of the box" on any normal system.
我不确定你正在使用什么服务器(例如Rails,Django,......),但这应该在任何正常系统上“开箱即用”。
#2
3
Using '&url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url
使用'&url ='+ encodeURIComponent(url);将URL从浏览器传递到服务器将对url进行编码
Yes, that's what you should be doing. encodeURIComponent
is the correct way to encode a text value for putting in part of a query string.
是的,这就是你应该做的。 encodeURIComponent是编码文本值以放入查询字符串的一部分的正确方法。
but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.
但是当它在服务器上解码时,url的参数被解释为单独的参数而不是单个url参数的一部分。
Then the server is very broken indeed. If that's really what's happening, you need to fix it at the server end.
然后服务器确实非常破碎。如果这真的发生了什么,你需要在服务器端修复它。
Code?
#3
-1
Use escape() to url encode it, it will encode the ampersands so that does not happen.
使用escape()对url进行编码,它将对&符号进行编码,这样就不会发生。
#4
-4
Honestly, go with Google URL Shortener. Then you can just use the URL code in the url query string: http://example.com/url/A7dh3
老实说,请使用Google URL Shortener。然后,您只需使用网址查询字符串中的网址代码:http://example.com/url/A7dh3
In your application, take that and prepend the Google URL Shortener domain name, and do the redirect. This adds tracking to the URL through Google Analytics also. Lots of advantages in this approach. Just a short code and added tracking data, too.
在您的应用程序中,取出并添加Google URL Shortener域名,然后执行重定向。这也会通过Google Analytics添加对网址的跟踪。这种方法有很多优点。只是一个简短的代码和添加的跟踪数据。
#1
48
encodeURIComponent()
should work. For example,
encodeURIComponent()应该工作。例如,
'&url=' + encodeURIComponent("http://a.com/?q=query&n=10")
produces
"&url=http%3A%2F%2Fa.com%2F%3Fq%3Dquery%26n%3D10"
(which doesn't have any &
or ?
in the value). When your server gets this url, it should be able to decode that to get the original:
(在值中没有任何&或?)。当您的服务器获取此URL时,它应该能够解码它以获取原始:
param["url"] = "http://a.com/?q=query&n=10"
I'm not sure what server you're using (e.g. Rails, Django, ...) but that should work "out of the box" on any normal system.
我不确定你正在使用什么服务器(例如Rails,Django,......),但这应该在任何正常系统上“开箱即用”。
#2
3
Using '&url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url
使用'&url ='+ encodeURIComponent(url);将URL从浏览器传递到服务器将对url进行编码
Yes, that's what you should be doing. encodeURIComponent
is the correct way to encode a text value for putting in part of a query string.
是的,这就是你应该做的。 encodeURIComponent是编码文本值以放入查询字符串的一部分的正确方法。
but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.
但是当它在服务器上解码时,url的参数被解释为单独的参数而不是单个url参数的一部分。
Then the server is very broken indeed. If that's really what's happening, you need to fix it at the server end.
然后服务器确实非常破碎。如果这真的发生了什么,你需要在服务器端修复它。
Code?
#3
-1
Use escape() to url encode it, it will encode the ampersands so that does not happen.
使用escape()对url进行编码,它将对&符号进行编码,这样就不会发生。
#4
-4
Honestly, go with Google URL Shortener. Then you can just use the URL code in the url query string: http://example.com/url/A7dh3
老实说,请使用Google URL Shortener。然后,您只需使用网址查询字符串中的网址代码:http://example.com/url/A7dh3
In your application, take that and prepend the Google URL Shortener domain name, and do the redirect. This adds tracking to the URL through Google Analytics also. Lots of advantages in this approach. Just a short code and added tracking data, too.
在您的应用程序中,取出并添加Google URL Shortener域名,然后执行重定向。这也会通过Google Analytics添加对网址的跟踪。这种方法有很多优点。只是一个简短的代码和添加的跟踪数据。