$ http service cache when method is submitted

when I set $ http caching requests, I still see duplicate requests (with the same URLs and the same data) sent to the server from the browser network,

$http.post(url, data, {cache:true} ).success(function(response) {

I have the following questions:

  • Is this the right behavior?
  • Can I cache mail requests?
  • Is this the right way to do this, or should I do it manually using $ cachefactory?
+4
source share
3 answers

From docs :

Only GET and JSONP requests are requested.

POST-, . / factory, $http. $cacheFactory .

function cacheService($http, $q){      
  var cache = {};      
  this.callSomething = function(postData){
    let deferred = $q.defer();
    let hash = angular.toJson(postData);
    if(cache[hash]){
      deferred.resolve(cache[hash]);
    } else {
      $http.post('path/to/resource', postData).then(function(response){
        cache[hash] = response;
        deferred.resolve(response);
      });
    }        
    return deferred.promise;
  }
}

, , , , URL, postData , .

+3

. $cacheFactory .

app.factory('Cache', function ($cacheFactory) {
    return $cacheFactory('Cache');
});

app.controller('MyController', function ($scope, $http, Cache) {
    $http.post(url, data, {cache:Cache} ).success(function(response) {}
});

EDIT:

GET JSONP.

- - URL- , ; .

, , .

, , , .

.

+2

AngularJS , :

GET JSONP.

$http.get(url, {cache: true}) HTTP (created with $cacheFactory).

$cachefactory -. URL-, $http, ( ). , GET, URL.

POST , URL-, POST ( ). W3:

, POST, Request-URI.

, POST, URI.

, Cache-Control Expires.

, this SO .

0

Source: https://habr.com/ru/post/1657599/


All Articles