我怎么URL连接codeA的URL参数,它本身就是一个网址? [英] How do I URL Encode a URL parameter that is itself a URL?

查看:104
本文介绍了我怎么URL连接codeA的URL参数,它本身就是一个网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速背景 -

我想提出一个jQuery AJAX调用返回JSON响应服务我写的。该服务接受一个网站的网址(例如www.google.com,www.xyz.com/abc123)。请求的格式如下:

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

...其中的[url]为用户提供的网址(再次,像www.google.com/abc)

我要URL连接code中的参数,因为mysite.com/www.google.com是给我的错误。

我的问题是,所有的标准JavaScript编码功能实际上并没有连接$ C C的URL $。

一个例子:

 < HTML>
< HEAD>
<脚本>
的document.write(EN codeURIComponent =EN + codeURIComponent(www.google.com)+< BR />中);
的document.write(EN codeURI =EN + codeURI(www.google.com)+< BR />中);
的document.write(逃=+越狱(www.google.com));
< / SCRIPT>
< /头>
<身体GT;
< /身体GT;
 

...有以下的输出:

 连接codeURIComponent = www.google.com
EN codeURI = www.google.com
逃生= www.google.com
 

什么是正确的方式来实现这一目标使用JavaScript / jQuery的?

我不认为这是相关的,但为了以防万一,这是一个Rails 3.0.7应用程序。

编辑更详细

如果www.google.com已经URL连接codeD和句点罚款,我的网址(www.mysite.com/www.google.com),为什么我收到这个错误?

从Chrome浏览器开发工具:

  GET的http://本地主机:3000 / S / www.google.com 404(未找到)
 

我的jQuery的片断:

  $的getJSON(HTTP://本地主机:3000 / S /。+ EN codeURI($(#txtURL)VAL()),功能(数据) {
        警报(data.result.url);
    });
 

解决方案

这是一个完全有效的URL:

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

我怀疑你只是运行到Rails的格式的东西(即的.html 在年底该URL设置格式为HTML,的.js 的JSON,...),所以你只需要修正路线,保持了格式自动检测功能的的方式获得

  map.connect/ S /:URL,:需求=> {:URL => /.*/},...
 

 匹配/ S /:URL => 煎饼#的房子',:限制=> {:URL => /.*/},...
 

或任何路由语法你使用。

如果你不告诉轨的:网址应与 /.*/ (即在任何所有的),它会尝试跨preT中的路由作为格式说明符的时期,这将失败,Rails会404,因为它无法弄清楚如何航线的URL。

Quick background -

I am making a jQuery ajax call to a service I wrote that returns a JSON response. The service accepts a web site URL (i.e. 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)

I need to URL encode the parameter, as mysite.com/www.google.com is giving me errors.

My problem is, all of the standard javascript encoding functionality does not actually encode the 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?

I don't think it's relevant, but just in case, this is a Rails 3.0.7 app.

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?

From Chrome Dev Tools:

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

My jQuery snippet:

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

解决方案

This is a perfectly valid URL:

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

I suspect that you're just running into the Rails format stuff (i.e. .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:

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 /.*/ (i.e. 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.

这篇关于我怎么URL连接codeA的URL参数,它本身就是一个网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆