ajax和相对网址

时间:2013-03-28 17:05:43

标签: jquery ajax xmlhttprequest

我真的不明白。我有以下内容' get'请求:

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

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

为什么'记者'我的路径的一部分被丢弃在登台服务器上?在查看文档时,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.

5 个答案:

答案 0 :(得分:11)

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

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

http://atlas/Reporter  

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

http://atlas/_relative_url_passed_to_ajax

如果以/结尾:

http://atlas/Reporter/  

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

http://atlas/Reporter/_relative_url_passed_to_ajax

答案 1 :(得分:7)

在URL开头的

/使其成为绝对的。

您可能需要相对网址,即

api/getdirectories/

getdirectories/

../api/getdirectories/

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我有同样的问题,我想我已经修复了它,因为我的一些AJAX URL已经在相对URL上工作了,但是我很困惑为什么我的一些AJAX调用不能按预期工作。这是我的第一个问题:

$.ajax({
            url: '/Controller/GetFiles',
            type: 'GET',
            dataType: 'json',
            data: { id: id },
            success: function (data) {
               //Business Logic Here
            },
            error: function (err) {
            },
        });

最初,我的网址是/Controller/GetFiles'。但是,将其部署到需要相对URL的登台站点时,将无法使用。因此为了修复它。我需要删除网址上的/

但是后来我发现我的某些具有相同格式的URL仍然无法使用。解决方案?在我的URL末尾添加其他/。这是结束格式:

$.ajax({
            url: 'Controller/GetFiles/',   
            type: 'GET',
            dataType: 'json',
            data: { id: id },
            success: function (data) {
               //Business Logic Here
            },
            error: function (err) {
            },
        });
相关问题