0

I'm adding/deleting dynamically items in my page "Items" with AJAX. But when I'm going to next page and then go back in my "Items", this collection is shown as it was before changing.

After F5-Refresh anything works.

How to avoid it? All is appropriate saved, but it looks like cache or smth like that.

Code sample:

  <script>

    delete_item_from_list = function (itemIdGiven, listIdGiven) {
        $.ajax({
            url: "@{Lists.deleteByIndexItem()}",
            method: 'post',
            async: true,
                    dataType: 'html',
            data: {
                itemId : itemIdGiven,
                listId : listIdGiven
            },
            success: function(data) {
                $(".items").html(data);
            },
            error: function() {
                console.log ("It's a TRAP!");
            }

        });
    };

</script>
sdf
  • 79
  • 2
  • 8
  • Please provide additional details and the code you use. – Alp Mar 30 '11 at 09:38
  • In code on delete/add I send request for changes to collection and as responce take the all collection as html. AJAX -> as responce you have XML **AJAH** -> as responce you have Html – sdf Mar 30 '11 at 09:40
  • caching behaviour are determined in http headers, not in javascript. Look for "http cache headers" maybe. That's all I can suggest given the vague question details you provided. – BiAiB Mar 30 '11 at 09:42
  • are you mentioning browser back button? – Harish Mar 30 '11 at 09:44
  • @Tomalak AJAH is the cousin of AJAX – nonopolarity Mar 30 '11 at 09:46
  • You should never use `async:false`. It may result in the browzer freezing until the request finished. Besides that, adding the `dataType` argument is usually a good idea. – ThiefMaster Mar 30 '11 at 09:46
  • @sdf: LOL, no "AJAH" doesn't exist. You're still pulling XML, even though it looks like HTML. Even "AJAX" is a mis-nomer as you're not always dealing with asynchronous calls. The entire enterprise is better called XmlHttpRequest. – Lightness Races in Orbit Mar 30 '11 at 10:31
  • @ThiefMaster: No, "never" is (a) wrong, (b) dangerous. Modern browsers will not lock up waiting for a synchronous request any more than they will lock up when you perform a normal, bog-standard page request. Sometimes a synchronous solution is correct: don't complicate your code with asynchronous spaghetti if you don't need it. – Lightness Races in Orbit Mar 30 '11 at 10:32

1 Answers1

0

This is an issue even right on the StackOverflow website:

Just try adding a comment as the first few ones, and then, you will see the comment added, and if you click your name to see your profile, and then click "Back", you won't see your comment that was added.

A way to handle this is by adding AJAX history and bookmark, so that, when AJAX was successful, the URL is actually changed, and bookmarkable (and in history). And when you click to a new page, and click "Back", the URL is suppose to become the old one, and the mechanism of the AJAX Bookmark and History should populate your content back for you.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • *so that, when AJAX was successful, the URL is actually changed* It looks really interesting, just like add some parameter to current URL after success? – sdf Mar 30 '11 at 09:57
  • @sdf: Yes, usually achieved via hashes. See [here](http://stackoverflow.com/questions/3849758/how-does-facebook-rewrite-the-source-url-of-a-page-in-the-browser-address-bar) for some suggestions on how to do it. – Lightness Races in Orbit Mar 30 '11 at 10:34