6

I have a little issue with my site. I have a page that hosts a google map. However the map does not get shown until the user clicks a button. It then call $.getJSON to get the addresses that i need to show on the map...

$.getJSON(theurl, function(json) {
  ...
}

It all works fine. However if the user then moves to a different page and then clicks the Back button they get the data from the $.getJSON call displayed, not the page itself.

It's as if the call to get the addresses has become part of the browsing history. If the user hits refresh when the data appears the full page then gets displayed.

Can anyone tell me how to stop this from happening.

I'm using the googlemap in an ASP.Net MVC site.

Thanks

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
Richard
  • 87
  • 1
  • 7
  • Are you modifying the history of the browser? – Jan Hančič Jan 10 '10 at 14:53
  • 1
    Is the url the same as the page? (ie, changing between html and json depending on if the request is deemed xhr) – meandmycode Jan 10 '10 at 18:48
  • Hi, thank you for your responses. I am not altering the history no, and yes the URL is the same but i check if it's an ajax request in teh server code so i know to dish up the json data and not the page. – Richard Jan 13 '10 at 13:26

3 Answers3

10

I solved it by including the below code just before the $.ajax function

$.ajaxSetup({ cache: false });

It works! Try it

Magesh
  • 760
  • 7
  • 16
  • 2
    This call changes the behavior of all calls to ajax on the page. This could lead to some very confusing behavior if you're not aware of it. – Wheeyls Sep 16 '12 at 22:33
4

This worked for me, and plays nicely with Rails controllers:

$.getJSON(url, {format: 'json'}, successHandler);

As a bonus I get to keep the cache.

I'm pretty sure this works for the same reason that cache: false works - both add a parameter to the url so that Chrome doesn't get confused by the origin. In this case though I get to cache the results, which is nice.

Wheeyls
  • 1,012
  • 1
  • 7
  • 14
3

This is actually a bug in Chrome. I fixed it by appending something to the URL of the AJAX call so it is different from the page URL (e.g. '&ajax=true').

So in your case it would look like:

$.getJSON(theurl + '&ajax=true', function(json) {
  ...
}

(And of course you'll need to check for '?' and what not in the querystring, but this illustrates the point.

phreakhead
  • 14,721
  • 5
  • 39
  • 40