Pressing the browser back button shows JSON, not HTML (using Rails & d3.js)

I generate a diagram in Rails using the d3.js JSON callback as follows:

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

Everything works perfectly. However, when the user leaves this diagram and then navigates to it using the back button on their browser, they are presented with JSON, not HTML.

Can you suggest how I can fix this?

+5
source share
2 answers
d3.json(window.location.pathname + ".json" + window.location.search.substring(0), function(json){  
    // generate chart
}

How inspired this question .

+2
source

, , "", URL-. - JSON, AJAX (D3).

- .html URL . , ApplicationController:

   before_filter do
     if request.format == :html && !params[:format]
       redirect_to format: :html
     end
   end

Vary: Accept, - Chrome , .

+4

All Articles