How to work with Ajax with JQuery?

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's called, it takes a while to get the results, but the next few times it works quickly. But I discovered that if I compile this code in a Cordova project, even though it is called again and there are different values being returned from the request, this method does not update the results. What can I do to resolve this?

    
asked by anonymous 09.06.2015 / 04:43

1 answer

2

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

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

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

2 - Put a timestamp in the url for the url to be always different and force the browser to make the request without cache. For example:

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