2

I am having trouble caching a JSONP request.

I have tried $http.jsonp(url, { cache: true }) and it doesn't appear to be working.

I also tried $http({ method: 'JSONP', url: url, cache: true }) to no avail.

Instead, I've resorted to manually caching the results (very very rough working example below).

Is it possible for AngularJS to do this caching for me?

countries.factory 'Wikipedia',
  ['$http', '$q', ($http, $q) ->
    cache = {}

    getSummary: (country) ->
      if cache.hasOwnProperty(country)
        cache[country]
      else
        summary = $q.defer()
        url = "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&rvsection=0&rvparse=1&titles=#{country}&format=json&redirects=1&callback=JSON_CALLBACK"

        $http.jsonp(url).success (data) ->
          # process data ....
          paragraphs = ['p1', 'p2']

          # return summary content paragraphs
          cache[country] = paragraphs
          summary.resolve paragraphs

        summary.promise
  ]
1
  • by "not working" you mean you see multiple requests in your inspector? Got a repro fiddle/plnker?
    – Ven
    Commented May 26, 2013 at 19:04

1 Answer 1

2

As of version 1.0.7/1.1.5 it looks like $http only supports caching for GET method requests.

  • Here is a link to an open github issue about it.
  • Here is a closed pull request with some additional information.

The caching section for the $http documentation only mentions GET requests as being cached.

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.