【发布时间】: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