jQuery.get returns old data

Related searches

I have a control page for my site that allows the admin to change various things about the site. One section allows for the admin to change pages. I use jQuery.get on the page selected to find the contents, then display that in a textarea. This all works fine, and the admin can modify the content and save it. The content IS saved, and the page DOES change once refreshed. However, when I use jQuery.get again to find the new contents of the page, it comes back with the original page content, the old data. No matter how many times I refresh the page the textarea always shows data that shouldn't exist anymore. Here is the isolated edit page code:

<script type="text/javascript">

$(document).ready(function() {

    $('#editpage').click(function() {

        var page = $('#page').val();

        $.get('pages/' + page + '.txt',{},function(data) {

            $('#display').val(data);

        });

    });

    $('#savepage').click(function() {

        var pageToChange = $('#page').val();
        var contents = $('#display').val();

        $.post('changepage.php',{page:pageToChange,text:contents},function(data) {

            $('#saveyay').html('<div class="success">Saved Changes</div>');

        });

    });

});

</script>

And changepage.php:

<?php

    if (array_key_exists('text',$_POST) AND array_key_exists('page',$_POST)) {

        file_put_contents('pages/'.$_POST['page'].'.txt', $_POST['text']);

    }

?>

What have I missed?

A possible reason for this is caching of the get request.

So try setting cache: false using $.ajaxSetup() before the get request()

$.ajaxSetup({
    cache: false
});

Setting it using ajaxSetup() will set the cache property to false for every ajax request.

If you want to set the property for only one request then you can use $.ajax() instead of $.get() with the custom options

javascript - jQuery.get returns old data, A possible reason for this is caching of the get request. So try setting cache: false using $.ajaxSetup() before the get request() $.ajaxSetup({ cache: false });. Since jQuery 1.4.3, data-* attributes are used to initialize jQuery data. An element's data-* attributes are retrieved the first time the data() method is invoked upon it, and then are no longer accessed or mutated (all values are stored internally by jQuery).

Your browser caching the results.

Switch from get to ajax and pass cache: false as one of your options.

cache (default: true, false for dataType 'script' and 'jsonp') Type: Boolean If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

jQuery.get(), jQuery.get( url [, data ] [, success ] [, dataType ] )Returns: jqXHR. Description: Load data from the server using a HTTP GET request. Definition and Usage. The data() method attaches data to, or gets data from, selected elements. Tip: To remove data, use the removeData() method.

This is because you're html cache is being saved locally.

On the start of your javascript file place:

$.ajaxSetup({
    cache: false
});

jQuery.data(), Store arbitrary data associated with the specified element and/or return the We can set several distinct values for a single element and retrieve them later:� data − This optional parameter represents key/value pairs that will be sent to the server. callback − This optional parameter represents a function to be executed whenever the data is loaded successfully.

browser cache is problem try do some thing like add ?_= new Date().getTime() to url

jQuery.map(), jQuery.map( array, callback )Returns: Array. Description: Translate all items in an array or object to new array of items� Note: This is a low-level method; a more convenient .data() is also available. The jQuery.data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore free from memory leaks. jQuery ensures that the data is removed when DOM elements are removed via jQuery methods, and when the user leaves the page.

jQuery.ajax(), jQuery.ajax( url [, settings ] )Returns: jqXHR A function to be used to handle the raw response data of XMLHttpRequest. setRequestHeader( name, value ) which departs from the standard by replacing the old value with the new one rather� Hi Fred, Indeed the view method returns the data items which correspond to the current page, but data returns the service response. In the provided DataSource configuration a page size is specified, which means that a request will only take a portion of 12 items.

The More General Method. As already mentioned, the $.ajax method is the real deal for any (not only JSON related) web request. This method allows us to explicitly set all the options we care about

jQuery - Get and Format Current Date using jQuery In this example we will learn how to get current date and display in different formats using jQuery. We will print date in DD/MM/YYYY, MM/DD/YYYY or DD MON YYY format on button clicks.

Comments
  • Fix your caching headers.
  • Or on that particular request
  • But you can't tell all users to do that. A solution in the code is what is required.
  • Disabling the cache on the client-side is not the solution.
  • The answer has been updated - possibly copied from someone else's - since we made our comments.
  • Sorry I misread the question thinking he just got it while testing.
  • @Popnoodles I wrote that without even seeing other peoples answers and your comment, I didn't know someon else had already posted this. I got it from a google search. You shouldn't come to conclusions that fast.