jQuery ajax 和相对 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15687907/
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
ajax and relative urls
提问by Nicros
I really don't get this. I have the following 'get' request:
我真的不明白这个。我有以下“获取”请求:
$.ajax( {
url: "api/getdirectories/",
dataType: 'json',
success: function ( data ) {
// Do stuff
}
} );
This is in a page served up to me by my staging server, http://atlas/Reporter
. On my local dev box, this works, and in looking at fiddler I see the correct URL http://localhost/Reporter/api/getdirectories
. On the staging server however the request is to http://atlas/api/getdirectories
, which is invalid and I get a 404 error.
这是在我的登台服务器提供给我的页面中,http://atlas/Reporter
. 在我的本地开发人员框中,这有效,并且在查看 fiddler 时,我看到了正确的 URL http://localhost/Reporter/api/getdirectories
。然而,在登台服务器上,请求是http://atlas/api/getdirectories
,这是无效的,我收到 404 错误。
Why is the 'Reporter' part of my path being dropped on the staging server? In looking at the document, the URL, baseURI, documentURI are all http://atlas/Reporter
, and the domain is atlas
. Exactly the same as on my local dev box (except it is all 'localhost' instead of 'atlas'.
为什么我的路径中的“Reporter”部分被丢弃在登台服务器上?在查看文档时,URL、baseURI、documentURI 都是http://atlas/Reporter
,域是atlas
。与我的本地开发箱完全相同(除了它都是“localhost”而不是“atlas”。
This problem has plagued me for a while now. I think I have it figured out and all is good, and then I run into it again. I don't think it is a same origin policy issue, as I am requesting the data from the same site the page originated from.
这个问题已经困扰我一段时间了。我想我已经弄清楚了,一切都很好,然后我再次遇到了它。我不认为这是同源策略问题,因为我从页面起源的同一站点请求数据。
So, how exactly is the full url determined for the request? It doesn't seem to be one of the document variables mentioned above being concatenated onto my relative URL... so how does this work?
那么,请求的完整 url 究竟是如何确定的呢?它似乎不是上面提到的连接到我的相对 URL 的文档变量之一......那么这是如何工作的?
Edit: Removed the '/' from the URL as it is distracting from the real issue- behavior is the same with or without it.
Edit: Removed the '/' from the URL as it is distracting from the real issue- behavior is the same with or without it.
回答by Florin D
Not sure if this is related, but when jQuery ajax builds relative urls, it matters if the current page url ends with "/" or not
不确定这是否相关,但是当 jQuery ajax 构建相对 url 时,当前页面 url 是否以“/”结尾很重要
If you invoke a page without an ending /
:
如果您调用一个没有结尾的页面/
:
http://atlas/Reporter
the relative urls from ajax calls on that page are ignoring the last segment:
该页面上 ajax 调用的相对 url 忽略了最后一段:
http://atlas/_relative_url_passed_to_ajax
If you end with /
:
如果你以/
:
http://atlas/Reporter/
the relative urls are using the last path segment Reporter
:
相对 url 使用最后一个路径段Reporter
:
http://atlas/Reporter/_relative_url_passed_to_ajax
回答by Denys Séguret
/
at the start of an URL makes it absolute.
/
在 URL 的开头使它成为绝对的。
You probably want a relative URL, that is
您可能想要一个相对 URL,即
api/getdirectories/
or
或者
getdirectories/
or
或者
../api/getdirectories/
回答by mlwacosmos
check the document_root on your server or if you have some virtual servers declared
检查您服务器上的 document_root 或者您是否声明了一些虚拟服务器
回答by Sheo Sagar
<html>
<head>
<title>tst</title>
</head>
<body>
<h1>Data list</h1>
<table border="1" width="50%" id="tblData">
<tr>
<th>Id</th>
<th>Name</th>
<th>Location</th>
</tr>
</table>
<script src="jquery.min.js"></script>
<script>
$(function(){
$.ajax({
url:'datas.txt',
type:'post',
datatype:'json',
success:function(response){
var myDataBunch = $.parseJSON(response);
var txt = '';
for(var i=0; i<myDataBunch.length; i++){
txt+= '<tr><td>'+ myDataBunch[i].id +'</td><td>'+ myDataBunch[i].name +'</td><td>'+myDataBunch[i].location+'</td></tr>'
}
$('#tblData').append(txt);
}
})
});
</script>
</body>
</html>