ajax和相对url

我真的不明白。 我有以下’获取’请求:

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

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

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

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

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

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

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


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

 http://atlas/Reporter 

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

 http://atlas/_relative_url_passed_to_ajax 

如果你以/结尾:

 http://atlas/Reporter/ 

相对url正在使用最后一个路径段Reporter

 http://atlas/Reporter/_relative_url_passed_to_ajax 

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

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

 api/getdirectories/ 

要么

 getdirectories/ 

要么

 ../api/getdirectories/ 

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

   tst   

Data list

Id Name Location