在AJAX请求相对URL [英] Relative URLs in AJAX requests

查看:133
本文介绍了在AJAX请求相对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Javascript的处理相对URL不同于标准的HTML?认为以下网址(或只是浏览到它): http://en.wikipedia.org/wiki/Rome 。 打开Firebug的控制台(或其他JavaScript控制台),并输入以下内容:

Why does Javascript treat relative URLs differently than standard HTML? Think of the following URL (or just browse to it): http://en.wikipedia.org/wiki/Rome. Open a Firebug console (or another Javascript console) and enter the following:

var x = new XMLHttpRequest();
x.open("GET", "foo", true);
x.send("bar");

在我的系统中的请求被发送到http://en.wikipedia.org/wiki/foo。 URL中的罗马被忽略。同样的请求,在URL(http://en.wikipedia.org/wiki/Rome/)斜线追加富,以完整的URL。

Under my system the request is sent to "http://en.wikipedia.org/wiki/foo". The "Rome" in the URL is simply ignored. The same request with a trailing slash in the URL ("http://en.wikipedia.org/wiki/Rome/") appends the "foo" to the full URL.

这似乎让人很难pretty的为en code在Javascript中正确的URL。是否有任何JavaScript库,以帮助解决这个问题?

This seems to make it pretty hard to encode the correct URLs in Javascript. Are there any Javascript libraries that help to overcome this problem?

(我问了一个类似的<一个href="http://stackoverflow.com/questions/4754559/relative-url-does-not-get-resolved-correctly-by-jquery">question之前,但更具体的jQuery,如果这也正好。我希望我能有这个较为库独立的问题一个更好的答案。)

(I asked a similiar question before, but more jQuery specific, where this also happens. I hope I get a better answer with this somewhat more library independent question.)

推荐答案

(更新,使其更具可读性)

(updated to make it more readable)

这是怎样的相对路径应该是工作。

This is how relative paths is supposed to work.

pretend,目前的地址是这样的:

Pretend that the current address is this:

绝对:协议://some.domain.name/dir1/dir2/filename

如果只指定一个新的文件名富,你会得到相同的协议,主机和显示目录,只有文件名改为:

If you specify only a new filename "foo", you get the same protocol, host and dirs, only the file name is changed:

相对

绝对:协议://some.domain.name/dir1/dir2/foo

如果您指定一个完整路径/ DIR3 /文件名2,你会得到相同的协议,主机名,但与另​​一个路径:

If you specify a whole path "/dir3/filename2" you get the same protocol and hostname but with another path:

相对 / DIR3 /文件名2

绝对:协议://some.domain.name/dir3/filename2

您也可以指定主机名//another.domain.name/dir5/filename3,并获得相同的协议,但另一台主机,DIR和文件名:

You can also specify host name "//another.domain.name/dir5/filename3" and get the same protocol but another host, dir and filename:

相对 // another.domain.name/dir5/filename3

绝对:协议://another.domain.name/dir5/filename3

可能是什么困惑的是,Web服务器内部可以添加/在URL结束时,如果指定的URL指向一个目录,而不是一个文件。

What might be confusing is that a webserver internally can add a / at the end of the url if the specified url points to a directory and not to a file.

协议://some.domain.name/somename

如果somename是目录中的Web服务器可能将其翻译成(可能与重定向)

If "somename" is a directory the webserver might translate it to (possible with a redirect)

协议://some.domain.name/somename/

更新

由于卡梅伦在注释中说:作为参考,请在的 RFC 1808

As cameron said in a comment: For reference, see step 6 in section 4 of RFC 1808

这篇关于在AJAX请求相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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