How to work with Ajax with Jquery?

Asked

Viewed 96 times

4

I have the following function:

function getJson() {

        $.ajaxSetup({
            async : false
        });

        $.getJSON(baseURL + "/ObterCursos",
                function(data) {
                        $.each(data, function(key, val) {
                            items.push({
                                "Codigo" : val.Codigo,
                                "Nome" : val.Nome
                            });
                });
        });
}

The first time it is called, it takes a while to bring the results, but the next times it works quickly. But I found that if I compile this code into a project Cordova, even if called again and there are different values being returned from the request, this method does not update the results. What can I do to fix this?

1 answer

2


This may be a cache related problem. To test/solve you can do 2 things:

1-uncheck the Jquery cache. Set cache: false, in the ajax setup.

$.ajaxSetup({
    async : false,
    cache: false
});

2- puts a timestamp on the url so the url is always different and forces the browser to make the request without cache. For example:

var timestamp = new Date().getTime();
$.getJSON(baseURL + "/ObterCursos?ts=" + timestamp, function(data) {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.