简体   繁体   English

如何URL编码本身就是URL的URL参数?

[英]How do I URL Encode a URL parameter that is itself a URL?

Quick background - 快速背景-

I am making a jQuery ajax call to a service I wrote that returns a JSON response. 我正在对我编写的返回JSON响应的服务进行jQuery ajax调用。 The service accepts a web site URL (ie www.google.com, www.xyz.com/abc123). 该服务接受网站URL(即www.google.com,www.xyz.com / abc123)。 The format of the request is as follows: 请求的格式如下:

http://www.mysite.com/[url]

... where [url] is a user provided URL (again, something like www.google.com/abc) ...,其中[url]是用户提供的URL(同样,例如www.google.com/abc)

I need to URL encode the parameter, as mysite.com/www.google.com is giving me errors. 我需要对参数进行URL编码,因为mysite.com/www.google.com给我错误。

My problem is, all of the standard javascript encoding functionality does not actually encode the URL. 我的问题是,所有标准javascript编码功能实际上均未编码URL。

An example: 一个例子:

<html>
<head>
<script>
document.write("encodeURIComponent = " + encodeURIComponent("www.google.com") + "<br />");
document.write("encodeURI = " + encodeURI("www.google.com") + "<br />");
document.write("escape = " + escape("www.google.com"));
</script>
</head>
<body>
</body>

... has the following output: ...具有以下输出:

encodeURIComponent = www.google.com
encodeURI = www.google.com
escape = www.google.com

What is the proper way to achieve this using JavaScript/jQuery? 使用JavaScript / jQuery实现此目标的正确方法是什么?

I don't think it's relevant, but just in case, this is a Rails 3.0.7 app. 我认为这无关紧要,但以防万一,这是一个Rails 3.0.7应用程序。

EDIT FOR MORE DETAIL 编辑更多细节

If www.google.com is already URL encoded, and periods are fine in my URL (www.mysite.com/www.google.com), why am I getting this error? 如果www.google.com已经被URL编码,并且URL(www.mysite.com/www.google.com)中的句点很好,为什么会出现此错误?

From Chrome Dev Tools: 通过Chrome开发工具:

GET http://localhost:3000/s/www.google.com 404 (Not Found)

My jQuery snippet: 我的jQuery代码段:

$.getJSON("http://localhost:3000/s/" + encodeURI($("#txtURL").val()), function(data) {
        alert(data.result.url);
    });

This is a perfectly valid URL: 这是一个完全有效的URL:

http://mysite.com/s/www.google.com

I suspect that you're just running into the Rails format stuff (ie .html at the end of the URL sets the format to HTML, .js for JSON, ...) so you just need to fix your route to keep the format auto-detection from getting in the way: 我怀疑您只是遇到了Rails format (即,URL末尾的.html将格式设置为HTML,JSON为.js等),因此您只需要修复路由即可保留格式自动检测是否妨碍:

map.connect '/s/:url', :requirements => { :url => /.*/ }, ...

or 要么

match '/s/:url' => 'pancakes#house', :constraints => { :url => /.*/ }, ...

or whatever routing syntax you're using. 或您使用的任何路由语法。

If you don't tell rails that the :url should match /.*/ (ie anything at all), it will try to interpret the periods in the route as format specifiers, that will fail and Rails will 404 because it can't figure out how to route the URL. 如果您不告诉Rails :url应该匹配/.*/ (即所有内容),它将尝试将路径中的句点解释为格式说明符,这将失败,并且Rails会404,因为它不能弄清楚如何路由URL。

URL encoding escapes characters that have a special meaning in the URL (like / and ? ) and/or aren't ASCII characters. URL编码转义在URL中具有特殊含义的字符(例如/? )和/或不是ASCII字符。 http://mysite.com/www.google.com is a perfectly valid URL, there's nothing to escape. http://mysite.com/www.google.com是一个完全有效的URL,没有任何可逃避之处。 If you'd include the protocol as well, you'd get some escape-worthy characters: 如果还包括该协议,则会得到一些值得转义的字符:

encodeURIComponent('http://www.google.com')
"http%3A%2F%2Fwww.google.com"

If your server 404s on a request to http://localhost:3000/s/www.google.com , that means the URL isn't handled by the server. 如果您的服务器404响应对http://localhost:3000/s/www.google.com的请求,则表明该服务器未处理该URL。 It doesn't mean that the URL is invalid. 这并不意味着URL无效。

As mu said dots don't need to be encoded. 如亩所说,点不需要编码。 The jQuery-way for building a proper QUERY_STRING would be the use of $.param() 用于构建适当的QUERY_STRING的jQuery方法是使用$.param()

$.param({encodeURIComponent:'http://www.google.de'})

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

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