ajax和相对网址

Nic*_*ros 5 ajax jquery xmlhttprequest

我真的不明白这一点.我有以下'获取'请求:

$.ajax( {
    url: "api/getdirectories/",
    dataType: 'json',
    success: function ( data ) {
        // Do stuff
    }
} );
Run Code Online (Sandbox Code Playgroud)

这是我的登台服务器提供给我的页面http://atlas/Reporter.在我的本地开发框中,这是有效的,并且在查看提琴手时,我看到了正确的URL http://localhost/Reporter/api/getdirectories.然而,在登台服务器上请求是http://atlas/api/getdirectories,这是无效的,我收到404错误.

为什么我的路径中的'Reporter'部分被丢弃在登台服务器上?在查看文档时,URL,baseURI,documentURI都是http://atlas/Reporter,域名是atlas.与我的本地开发盒完全相同(除了它是'localhost'而不是'atlas'.

这个问题困扰了我一段时间了.我想我已经弄明白了,一切都很好,然后再次遇到它.我不认为这是一个相同的原始政策问题,因为我要求页面来自同一网站的数据.

那么,为请求确定的完整网址究竟是如何确定的?它似乎不是上面提到的文档变量之一连接到我的相对URL ...所以这是如何工作的?

Edit: Removed the '/' from the URL as it is distracting from the real issue- behavior is the same with or without it.

Flo*_*n D 12

不确定这是否相关,但是当jQuery ajax构建相对url时,如果当前页面url以"/"结尾,则重要


如果您调用没有结尾的页面/:

http://atlas/Reporter  
Run Code Online (Sandbox Code Playgroud)

来自该页面的ajax调用的相对URL忽略了最后一个段:

http://atlas/_relative_url_passed_to_ajax
Run Code Online (Sandbox Code Playgroud)

如果你以/:

http://atlas/Reporter/  
Run Code Online (Sandbox Code Playgroud)

相对网址正在使用最后一个路径段Reporter:

http://atlas/Reporter/_relative_url_passed_to_ajax
Run Code Online (Sandbox Code Playgroud)


Den*_*ret 7

/ 在URL的开头使它成为绝对的.

您可能想要一个相对URL,即

api/getdirectories/
Run Code Online (Sandbox Code Playgroud)

要么

getdirectories/
Run Code Online (Sandbox Code Playgroud)

要么

../api/getdirectories/
Run Code Online (Sandbox Code Playgroud)


mlw*_*mos 0

检查服务器上的 document_root 或者是否声明了一些虚拟服务器