【问题标题】:ajax and relative urlsajax 和相对 url
【发布时间】:2013-03-19 05:58:54
【问题描述】:

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

$.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 究竟是如何确定的呢?它似乎不是上面提到的文档变量之一,它被连接到我的相对 URL 上……那么它是如何工作的?

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

【问题讨论】:

  • 你的网址不应该是api/getdirectories/而不是/api/getdirectories/吗?
  • 你试过 url:"api/getdirectories/",
  • 我试过api/getdirectories,效果一样。
  • 您的 HTML 文件的路径是什么,您在浏览器中使用什么 URL 来打开它?
  • @dystroy 正确的路径是http://atlas/reporter/api/getdirectores。这是托管在我的服务器上的 web.api 应用程序。

标签: jquery ajax xmlhttprequest


【解决方案1】:

不确定这是否相关,但是当 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

【讨论】:

    【解决方案2】:

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

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

    api/getdirectories/
    

    getdirectories/
    

    ../api/getdirectories/
    

    【讨论】:

    • 无论如何,Reporter 部分丢失了。就我而言,网址最终是相同的(错误的)地址。
    • 你的 HTML 文件的路径是什么,你在浏览器中使用什么 URL 来打开它?
    • 正确的路径是http://atlas/reporter/api/getdirectores。这是托管在我的服务器上的 web.api 应用程序。
    • 这解决了我的问题——去掉开头的斜线。谢谢。
    【解决方案3】:

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

    【讨论】:

    • 这很有趣——除了我不知道如何在我的 IIS 服务器上检查它......我在哪里可以找到它?
    • 我查看了我网站的 IIS 设置 - 我看到的唯一内容是高级设置中的“虚拟路径”。但是这个值在我的登台服务器和我的本地开发盒上是相同的。 :(
    • 对不起,我不知道 IIS,但一定有一个配置文件在某个地方......这是肯定的
    【解决方案4】:
    <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>
    

    【讨论】:

      【解决方案5】:

      我有同样的问题,我想我已经修复了它,因为我的一些 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 仍然无法正常工作。解决方案?在我的网址末尾添加额外的/。以下是结束格式:

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

      【讨论】:

        猜你喜欢
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 2013-03-03
        • 2011-01-22
        • 1970-01-01
        相关资源
        最近更新 更多