【问题标题】:Route passing in Sinatra for single page app with REST resources在 Sinatra 中为具有 REST 资源的单页应用程序传递路由
【发布时间】:2014-09-10 10:33:51
【问题描述】:

我正在尝试在后端使用 sinatra 制作单页网站。我希望所有带有首选接受标头“text/html”的 GET 请求都返回相同的页面,但所有希望 json 获取 json-data 的请求。

例子:

接受设置为“text/html”的“/users”的 GET 调用应返回 index.html。

接受设置为“应用程序/json”的“/用户”的 GET 调用应返回带有用户的 JSON 数据。

我尝试过对 html 使用包罗万象的方法并使用这样的接受检查:

# Generic html giver for angular routing
get //, :provides => 'html' do
  pass unless request.preferred_type.to_str == 'text/html'
  erb :index
end

# Give users as JSON
get '/users', :provides => 'json' do
  pass unless request.preferred_type.to_str == 'application/json'
  '["dummy", "array"]'
end

...但是当preferred_type 不是text/html 时,它似乎没有传递到第二条路线。

注意:我使用的是preferred_type 的字符串检查,因为使用request.accept?捕获所有将“*/*”作为最后一个接受标头的浏览器。

【问题讨论】:

    标签: sinatra single-page-application


    【解决方案1】:

    哦,看来罪魁祸首是:

    :provides => 'json'
    

    没有它,传递按预期工作。我猜那是一个错误。

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 2016-03-03
      • 2018-12-15
      • 2012-07-14
      • 2019-03-05
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多