J Hunt
J Hunt

Reputation: 791

Angular http get not refreshing data

When my page starts setDataOnPage2 executes and works fine. I'm able to get the data into $scope.records to present it to the page.

    $scope.setDataOnPage2 = function () {

    $http({
        method: 'get',
        url: GetSchedule,
        params: {
            date: $scope.currentDate
        }
    }).success(function (results) {
        console.log(JSON.stringify(results));
        $scope.records = results; 
    });
    }
    $scope.setDataOnPage2();

My problem is when I hit this function from a button using ng-click. When I click the button I see the results in a JSON presentation, because of console.log(JSON.stringify(results)), but it is old data.

When I open the form on my pc it works fine, I then go to my phone update the data fine, but when I click on the refresh button that executes setDataOnPage2 it shows the JSON in the console, but doesn't show the updates that I did on the phone until I refresh the page. I'm not sure why my http call is caching my data.

I even put a breakpoint in the code behind that the http get should be executing, but it only executes when the page refreshes, not when I click the refresh button that executes setDataOnPage2.

It is strange that the json shows up in the console, but the data isn't refreshed and it doesn't hit the breakpoint.

Any ideas on why this could be happening?

Upvotes: 2

Views: 3122

Answers (1)

Kathir
Kathir

Reputation: 6196

This is probably a caching issue on the browser. The browser might cache the get request. To avoid the issue just append the request url with a timestamp.

GetSchedule+"&random="+new Date().getTime() 

if you already have query parameters otherwise use

GetSchedule+"?random="+new Date().getTime() 

or you can also add headers to avoid the cache like

 headers: {
                'Cache-Control': 'no-cache',
                'Pragma':'no-cache'
            },

Upvotes: 7

Related Questions