Javascript AJAX 请求中的相对 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4765740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:58:37  来源:igfitidea点击:

Relative URLs in AJAX requests

javascriptajaxurl

提问by Zardoz

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:

为什么 Javascript 对待相对 URL 的方式与标准 HTML 不同?考虑以下 URL(或只是浏览它):http: //en.wikipedia.org/wiki/Rome。打开 Firebug 控制台(或另一个 Javascript 控制台)并输入以下内容:

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

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.

在我的系统下,请求被发送到“ http://en.wikipedia.org/wiki/foo”。URL 中的“Rome”被简单地忽略了。在 URL(“ http://en.wikipedia.org/wiki/Rome/”)中带有斜杠的相同请求将“foo”附加到完整 URL。

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?

这似乎使得在 Javascript 中编码正确的 URL 变得非常困难。是否有任何 Javascript 库可以帮助克服这个问题?

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

(我之前问过一个类似的问题,但更具体的 jQuery,这也会发生。我希望我能通过这个更独立于库的问题得到更好的答案。)

回答by some

(updated to make it more readable)

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

This is how relative paths is supposed to work.

这就是相对路径应该如何工作。

Pretend that the current address is this:

假设当前地址是这样的:

Absolute:protocol://some.domain.name/dir1/dir2/filename

绝对:protocol://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:

如果你只指定一个新的文件名“foo”,你会得到相同的协议、主机和目录,只是文件名改变了:

Relative:foo

相对的:foo

Absolute:protocol://some.domain.name/dir1/dir2/foo

绝对:protocol://some.domain.name/dir1/dir2/foo



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

如果您指定整个路径“/dir3/filename2”,您将获得相同的协议和主机名,但具有另一个路径:

Relative:/dir3/filename2

相对的:/dir3/filename2

Absolute:protocol://some.domain.name/dir3/filename2

绝对:protocol://some.domain.name/dir3/filename2



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”并获得相同的协议但另一个主机、目录和文件名:

Relative://another.domain.name/dir5/filename3

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

Absolute:protocol://another.domain.name/dir5/filename3

绝对:protocol://another.domain.name/dir5/filename3



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.

可能令人困惑的是,如果指定的 url 指向目录而不是文件,则网络服务器内部可以在 url 的末尾添加 /。

protocol://some.domain.name/somename

protocol://some.domain.name/somename

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

如果“somename”是一个目录,则网络服务器可能会将其转换为(可能通过重定向)

protocol://some.domain.name/somename/

protocol://some.domain.name/somename/



UPDATE

更新

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

正如卡梅伦在评论中所说:供参考,请参阅RFC 1808第 4 节中的步骤 6

回答by Partly Cloudy

Looks like http://code.google.com/p/js-uri/is the library of choice for URL manipulation in general and this type of absolute-to-relative computation in particular:

看起来http://code.google.com/p/js-uri/是一般 URL 操作的首选库,特别是这种绝对到相对计算:

new URI(potentiallyRelativeLink).resolve(new URI(window.location.href)).toString()