最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $.getJSONback button showing JSON return data not the page - Stack Overflow

programmeradmin3浏览0评论

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 bee 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

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 bee 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

Share Improve this question edited Feb 25, 2013 at 20:47 DaveRandom 88.7k11 gold badges158 silver badges173 bronze badges asked Jan 10, 2010 at 14:36 RichardRichard 871 silver badge7 bronze badges 3
  • Are you modifying the history of the browser? – Jan Hančič Commented Jan 10, 2010 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 Commented Jan 10, 2010 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 Commented Jan 13, 2010 at 13:26
Add a ment  | 

3 Answers 3

Reset to default 10

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

$.ajaxSetup({ cache: false });

It works! Try it

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.

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.

发布评论

评论列表(0)

  1. 暂无评论