问题描述
我正在使用jQuery(1.4.4)使用一些JavaScript构建Rails(3.0.3)应用程序。在其中一个网站上,我添加了一个click事件监听器,并希望使用AJAX请求加载一些内容。
I am building a Rails (3.0.3) application with some JavaScript using jQuery (1.4.4). On one of the sites I added a click event listener and want to load some content using a AJAX request.
$(document).ready(function() {
$("a#settings").click(function() {
$("div#box").load("settings.js");
return false;
});
});
当我现在打开网站http:// localhost:3000 / entities / 3时,链接与绑定的单击侦听器位于并单击链接我得到404错误。发生这种情况是因为AJAX请求使用URLhttp:// localhost:3000 / entities / settings.js而不是(我将要求)http:// localhost:3000 / entities / 3 / settings.js。
When I now open the site "http://localhost:3000/entities/3" where the link with the bound click listener is located and click the link I get a 404 error. This happens because the AJAX request uses the URL "http://localhost:3000/entities/settings.js" and not (as I would exect) "http://localhost:3000/entities/3/settings.js".
推荐答案
:
这是相对路径的工作方式。
This is how relative paths is supposed to work.
protocol://some.domain。 name / dir1 / dir2 / filename
如果只指定一个新文件名foo,则会获得相同的协议,主机和目录,只更改了文件名:
If you specify only a new filename "foo", you get the same protocol, host and dirs, only the file name is changed:
protocol://some.domain.name/dir1/dir2/foo
如果指定整个路径/ dir3 / filename2,则会获得相同的协议和主机名,但使用其他路径:
If you specify a whole path "/dir3/filename2" you get the same protocol and hostname but with another path:
protocol://some.domain.name/dir3/filename2
您还可以指定主机名//another.domain.name/dir5/filename3并获得相同的协议,但另一个主机,目录和文件名:
You can also specify host name "//another.domain.name/dir5/filename3" and get the same protocol but another host, dir and filename:
protocol://another.domain.name/dir5/filename3
可能令人困惑的是,如果指定的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.
protocol://some.domain.name/somename
如果somename是一个目录,则网络服务器将其转换为
If "somename" is a directory the webserver translates it to
protocol://some.domain.name/somename/
供参考,请参阅第4节中的步骤6
For reference, see step 6 in section 4 of RFC 1808
这篇关于jQuery无法正确解析相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!