1

i'm new to AngularJS, and i'm writing an app that recives JSON data with callback via $http jsonp method. Caching parameter is not set, but browser caches data. How can i solve that?

$scope.fetch=function () {
$http({method: 'JSONP', url: 'http://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero'})
.success(function(data) {
$scope.current =  data.salutation.toLowerCase();
console.log($scope.current);
$scope.dim=$scope.current; doIt();}).error(function(data) { 
$scope.current =   data.salutation.toLowerCase() || "Request failed";   $scope.dim=$scope.current;});

};

1 Answer 1

2

Not sure exactly what angular's doing, but this is typically done by appending a timestamp to the url. Change your original URL from:

'http://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero'

To:

'http://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero&_=' + (new Date().getTime())
Sign up to request clarification or add additional context in comments.

6 Comments

Thats the code i write .. $scope.fetch=function () { $http({method: 'JSONP', url: 'angularjs.org/…'}).success(function(data) { $scope.current = data.salutation.toLowerCase(); console.log($scope.current); $scope.dim=$scope.current; doIt();}).error(function(data) { $scope.current = data.salutation.toLowerCase() || "Request failed"; $scope.dim=$scope.current;}); };
Care to put that in your question so it can be properly formatted?
I changed the question... still do not understand why there is a caching enabled?
because jsonp is actually a javascript tag in the page with the source to whatever you want to load. and browsers cache javascripts unless told not to. what Travis is saying is to also append a dummy argument to the url with the timestamp. so when a new call is made, the browser will see the url as a different javascript and loads it accordingly
The caching option in Angular is a JavaScript based caching option. You are running into an issue where the browser is actually caching the request. The only way for your code to ensure it's getting new results each time is to append something unique to the URL each request, hence the timestamp. I'll edit my answer with a more precise response.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.