ajax和相对网址 [英] ajax and relative urls

查看:86
本文介绍了ajax和相对网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I really don't get this. I have the following 'get' request:

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

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

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.

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

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'.

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

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.

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

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中移除'/',因为它会分散真实问题 - 无论是否有相同的行为它。

推荐答案

不确定这是否相关,但是当jQuery ajax构建相对网址时,它重要的是当前页面网址以/结尾

Not sure if this is related, but when jQuery ajax builds relative urls, it matters if the current page url ends with "/" or not

如果你调用一个没有结尾的网页 /

If you invoke a page without an ending /:

http://atlas/Reporter  

该页面上ajax调用的相对网址忽略了最后一段:

the relative urls from ajax calls on that page are ignoring the last segment:

http://atlas/_relative_url_passed_to_ajax






如果你以<结尾code> / :

http://atlas/Reporter/  

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

the relative urls are using the last path segment Reporter:

http://atlas/Reporter/_relative_url_passed_to_ajax

这篇关于ajax和相对网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆