简体   繁体   English

将 url 作为 url 参数传递的推荐方法是什么?

[英]What is the recommended way to pass urls as url parameters?

Using &url='+encodeURIComponent(url);使用&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 从浏览器传递到服务器将对 url 进行编码,但是当它在服务器上被解码时,url 的参数被解释为单独的参数,而不是单个 url 参数的一部分。

What is the recommended way to pass urls as url parameters ?将 url 作为 url 参数传递的推荐方法是什么?

encodeURIComponent() should work. encodeURIComponent()应该可以工作。 For example,例如,

'&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 时,它应该能够对其进行解码以获取原始 URL:

param["url"] = "http://a.com/?q=query&n=10"

I'm not sure what server you're using (eg Rails, Django, ...) but that should work "out of the box" on any normal system.我不确定您使用的是什么服务器(例如 Rails、Django ……),但这应该可以在任何普通系统上“开箱即用”。

Using '&url='+encodeURIComponent(url);使用 '&url='+encodeURIComponent(url); to pass a URL from browser to server will encode the 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?代码?

I ran into this issue, personally I couldn't use any of the accepted answers, but it can also be done by just encoding the url into Base 64, passing it as a parameter, and then decoding it.我遇到了这个问题,我个人无法使用任何已接受的答案,但也可以通过将 url 编码为 Base 64,将其作为参数传递,然后对其进行解码来完成。 With javascript, you can encode a string s to base 64 with btoa(s) and decode with atob(s).使用 javascript,您可以使用 btoa(s) 将字符串 s 编码为 base 64,并使用 atob(s) 进行解码。 Other languages have ways of doing the same thing.其他语言有做同样事情的方法。 Base 64 is just kinda like representing a larger series of characters with 64 character(For ex, all the capital letters, all the lowercase, and a couple symbols). Base 64 有点像用 64 个字符(例如,所有大写字母、所有小写字母和几个符号)来表示更大的一系列字符。 Kinda like how we represent letters in Binary.有点像我们在二进制中表示字母的方式。 But it's nice to use, because then we can just pass base64 strings as parameters, and then they won't interfere/get interpreted in a weird fashion, and then we can decode them at the next stage.但是使用起来很好,因为这样我们就可以将 base64 字符串作为参数传递,然后它们就不会干扰/以奇怪的方式被解释,然后我们可以在下一阶段对它们进行解码。

使用 escape() 对它进行 url 编码,它将对 & 符号进行编码,这样就不会发生这种情况。

Honestly, go with Google URL Shortener.老实说,使用 Google URL Shortener。 Then you can just use the URL code in the url query string: http://example.com/url/A7dh3然后你可以在 url 查询字符串中使用 URL 代码: http : //example.com/url/A7dh3

In your application, take that and prepend the Google URL Shortener domain name, and do the redirect.在您的应用程序中,获取并添加 Google URL Shortener 域名,然后执行重定向。 This adds tracking to the URL through Google Analytics also.这也通过 Google Analytics 添加了对 URL 的跟踪。 Lots of advantages in this approach.这种方法有很多优点。 Just a short code and added tracking data, too.只是一个简短的代码并添加了跟踪数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM