Backbone: When I click the back button, json is render

Created on 24 Sep 2011  ·  10Comments  ·  Source: jashkenas/backbone

I have Rails app using backbone. When I click the back button or front button randomly I'll see json.

invalid

Most helpful comment

Thanks!
for me

$.ajaxSetup
  cache: false

was enough

All 10 comments

Can you provide more information? Many people are using Backbone in production with Rails at the moment and aren't seeing the same behavior.

Specifically please provide a test case so we can accurately reproduce the problem.

I am experiencing the same - whenever i navigate to external url (not handled by backbone app) or close the tab and then go back or re open the tab - i am seeing JSON. I am not sure how can I provide a test case, but I can share some details that might give you a clue what's happening.

the application uses _.delay function to sync model collection with the server (collection.fetch()). The server is a RoR app that serves both app and JSON for sync on the same URL and uses headers to decide if it needs to serve the whole app or just a JSON array of data for models. could that be the problem?

My other idea, is that browser caches the latest request it made to the server and when you press back it just renders it. If i manage to leave the app quickly (before first sync request) then going back renders the app (not the JSON).

Do you have any ideas how to overcome this?

I solved the problem by doing this

$.ajaxSetup
  beforeSend: (xhr) ->
    xhr.setRequestHeader "Accept", "application/json"
  cache: false

Thanks!
for me

$.ajaxSetup
  cache: false

was enough

+1

+1

I had the same problem in my RoR app and your solution worked for me.

Thanks @omenking @antonlitvinenko

Even better, always explicitly add .json extension to your urls, and let the browser cache both, json and page html (if you don't mind it), i.e. /users/1 + /users/1.json instead of overwriting cache entry for the same url. Otherwise you'll miss the benefits of browser caching (which is nice, right? ;)) Just my 2c.

@Sija you is right, but I changed to explicit .json and when I do test was received the same problem of json overwrite the page. Do you have any idea what I'm doing wrong? hehe
tks

+1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

etler picture etler  ·  13Comments

jamiebuilds picture jamiebuilds  ·  12Comments

jashkenas picture jashkenas  ·  7Comments

sarkasm picture sarkasm  ·  7Comments

gfranko picture gfranko  ·  18Comments