【问题标题】:Route in Rails for json depends on where called fromRails 中 json 的路由取决于从哪里调用
【发布时间】:2018-06-11 03:58:01
【问题描述】:

创建了一个文件maps.json,并在以下 JavaScript 中使用

function findSelectedMap(mapID, cb) {
   $.getJSON('maps.json', function(json) { 
    json.forEach(function(entry) {
      if (entry.maps.id == mapID) {
        changeLayerTo = entry.maps.url;
        maxZoom = entry.maps.zoom;
      }
    });
    cb();
  });
};

当从顶层http://localhost:3000/overview 调用时它工作正常,mapId

mapID = $("#select-overlay input[type='radio']:checked").val();

但是当从http://localhost:3000/streets/45 调用时,错误是http://localhost:3000/streets/streets/maps.json 404 (Not Found)。我在路线中尝试了get 'streets/maps.json' => 'maps.json' 和类似的东西。雷东多海滩没有成功。我想我可以强制 maps.json 位于 streets 并执行 if exists (无论 jS 是什么),但这似乎很麻烦。有没有办法解决这个问题?

【问题讨论】:

    标签: javascript ruby-on-rails json routes jbuilder


    【解决方案1】:

    您可以在端点的开头添加斜杠,让浏览器知道您的 URL 从基本 URL 开始

    $.getJSON('/maps.json', function(json) {
      // rest of logic
    })
    

    【讨论】:

    • 谢谢。我知道它必须很简单。但还是没有想到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多