goodeggs / angular-cached-resource

An AngularJS module to interact with RESTful resources, even when browser is offline
MIT License
216 stars 29 forks source link

$cachedResource fire an OPTION on load, which fail and repeat each X seconds. #60

Closed tplaindoux closed 9 years ago

tplaindoux commented 9 years ago

Well this is kind of an odd case, but I get a cycling option firing each X seconds (each 20s I'd say), which fail because... I didn't make any actual call.

This is the error :

 OPTIONS http://myAPI.com/ angular.js:9814 

(anonymous function)angular.js:9615
sendReq angular.js:9331
$get.serverRequest angular.js:13170
processQueue angular.js:13186 
(anonymous function ) angular.js:14383 
$get.Scope.$eval angular.js:14199 
$get.Scope.$digest angular.js:14488 
$get.Scope.$apply angular.js:16214 
(anonymous function) angular.js:4902 
completeOutstandingRequest angular.js:5282
(anonymous function)

XMLHttpRequest cannot load http://myAPI.com/. Invalid HTTP status code 405 (index):1 

Notice that this is an option call. Here is the code snippet creating the loop:

        Acaci: $cachedResource('acaci', API + ':ep/:s/:id/:extra',  {}, {
          get: {
            method:'GET',
            timeout: 1500,
            params: {
              s:'@s',
              ep:'@ep',
              id:'@id',
              extra:'@extra'
            }
          },
          create: {
            method:'POST',
            headers: {
              'Content-Type': 'application/json'
            },
            params: {
              s:'@s',
              ep:'@ep',
              id:'@id',
              extra:'@extra'
            }
          },
          erase: {
            method:'DELETE',
            headers: {
              'Content-Type': 'application/json'
            },
            params: {
              s:'@s',
              ep:'@ep',
              id:'@id',
              extra:'@extra'
            }
          },
          update: {
            method:'PUT',
            headers: {
              'Content-Type': 'application/json'
            },
            params: {
              s:'@s',
              ep:'@ep',
              id:'@id',
              extra:'@extra'
            }
          }
        }),

Replacing cachedResource by ressource stops the Option cycle, that's why I think this is from angular-cached-resource.

This happen on any page of the app, right on load.

hazeledmands commented 9 years ago

Hi @Xspirits! Sorry for the delayed response.

When you first load a $cachedResource, it checks local storage to see if there are any failed POST, PUT, or DELETE requests from the last time the page was loaded -- if there are, then it'll try and make the request again. Sounds like that's what's happening in your case.

I am guessing that you're seeing an OPTIONS request because it's trying to hit http://myAPI.com and you're running the page from a different server, like localhost. When you make requests like this, the browser will use Cross Origin Resource Sharing requests and headers to make sure that there's nothing fishy going on.

hazeledmands commented 9 years ago

The quickest way to solve your issue, or at least the symptom, is to clear your local storage cache.

You can also do this from your code with:

$cachedResource.clearCache()

But keep in mind that if you run this on every page load, you'll lose the benefit of a cached write running after the page refreshes.

hazeledmands commented 9 years ago

Feel free to reopen this issue if this doesn't solve your problem!

tplaindoux commented 9 years ago

Hello,

A quick update if anyone meet the same issue: Error was firing because of the timeoutparameter set and the api response taking a bit too long.

Actually, nothing's related to Angular cached resource ;) My bad, thanks for you help.

Also, for clearCache I only use it, per instance ( MyApiService.Name.clearCache() ) when I update some content, so I keep the benefits of