admin管理员组文章数量:1336659
I'm generating a chart in Rails using a d3.js JSON callback like this:
View
d3.json(document.URL, function(data){
// generate chart
}
Controller
def index
respond_to do |format|
format.html do
# return the HTML
end
format.json do
# return the JSON
end
end
end
All works fine. However when a user leaves this chart, and then navigates back to it using the "back" button on their brower they are presented with the JSON rather than the HTML.
Can you suggest how I might fix this?
I'm generating a chart in Rails using a d3.js JSON callback like this:
View
d3.json(document.URL, function(data){
// generate chart
}
Controller
def index
respond_to do |format|
format.html do
# return the HTML
end
format.json do
# return the JSON
end
end
end
All works fine. However when a user leaves this chart, and then navigates back to it using the "back" button on their brower they are presented with the JSON rather than the HTML.
Can you suggest how I might fix this?
Share Improve this question edited Sep 12, 2012 at 13:04 paxRoman 2,0623 gold badges19 silver badges32 bronze badges asked Aug 15, 2012 at 9:15 Derek HillDerek Hill 6,4645 gold badges58 silver badges79 bronze badges2 Answers
Reset to default 6Well, this is because when you hit back button the browser is serving last cached content for the given URL. IIn your case the last content is JSON requested by AJAX (D3).
In addition to accepted answer you can also try the other way - just append .html
to page URL. You can automate it by adding this filter to controller or ApplicationController
:
before_filter do
if request.format == :html && !params[:format]
redirect_to format: :html
end
end
There is also a third standard way with Vary: Accept
header but it may cause some troubles because of this Chrome bug and some problems described in The browser cache is Vary broken
d3.json(window.location.pathname + ".json" + window.location.search.substring(0), function(json){
// generate chart
}
As inspired by this question.
本文标签:
版权声明:本文标题:javascript - Hitting browser "back" button shows JSON rather than HTML (using Rails & d3.js) - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742420498a2471561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论