badbye badbye - 4 months ago 8
HTML Question

Javascript: Json file won't be resent when user goes to the same page again?

I have a search function looks like google, the way it works is like when someone get on search page, server then send a json file where includes object name and id, so when user type in something in the search box, the box would pop up the similar name, and when user click on one result, the associated id is the reference to direct user to the associated page using post method.

The problem is, after user insert a new object on a norther page, although the servers has update the database and jsonfile, when user back to the search page, the json file won't be sent again, so users won't see the newly inserted object name in the search result. So I wondered how I could fix this.

The code below is the code in the search page that sending the json file.

<script>
var obj;
$.getJSON( 'obdatabase.json', function(obj) {
obj=obj;
});
// some code for search.
</script>

Answer

Do you mean that it is being cached by the browser? A simple solution would be to include a unique querystring parameter:

<script>
var obj;
    $.getJSON( 'obdatabase.json?d=' + (new Date().getTime()), function(obj) {  
        obj=obj;
    });
 // some code for search.
</script>