1

In an Angular JS app I'm working on, I am using a service to periodically (in a $timeout) make a GET request to a URL in my API (both the Angular app and the API are being served from port 5000 on localhost).

For some reason, it appears that $http is not actually sending the GET. For each $http.get(), the .error() is called with empty data and a status of 0. When I check in my server log (I'm running a Ruby on Rails backend with the Unicorn gem for my server), it appears that the server never receives the request from Angular.

Here's the function in my service:

updateUserStatus = () ->
    $http.get('/api/v1/status').success (statusData) ->
        # update the variable and notify the observers
        this.userStatus = statusData
        notifyObservers()
        startStatusTimeout()
    .error (error, status) ->
        # if there's an error, log it
        console.log 'error:'
        console.log error
        console.log status
        startStatusTimeout()

What's really odd is that it only happens sometimes. When it stops working, I can change the URL in the $http.get() to '/api/v1/status.json', and it works. For a while. Then I switch it back and it works again, for a while... obviously there is some greater issue at play.

I've been racking my brain for a few days now, and I've seen a bunch of similar issues on SO, but they all seem to be solved with implementing CORS in Angular, which I don't think is applicable to my situation because it's all coming from localhost:5000. Am I wrong? What's going on?

For reference, I'm using Angular version 1.0.7.

7
  • 1
    Have you checked if the browser actually sends something out? That might provide a clue. Google Chrome has a network tab in its Developer Tools, for example. Keep it open and make the $http.get happen. See if a request goes out, and inspect it if so. Commented Aug 8, 2013 at 14:17
  • Thanks, I'll have to try that out. I just did the quick fix I described (changing the URL) so I'll test it when it stops working again. Commented Aug 8, 2013 at 14:20
  • Just checked the network tab - the browser never sends the GET request. For reference, I'm using the newest version of Chrome. When I load the page in Firefox, it successfully sends the GETs, but it might stop working after a while there too. Any ideas? It usually happens right after I save changes to the Javascript/Coffeescript files and refresh the page. Commented Aug 9, 2013 at 9:27
  • I'm still running into this issue. I haven't been able to replicate it in Firefox, so I'm guessing it's a Chrome problem. When I restart the browser, it works again, but just refreshing the page doesn't fix it. Commented Aug 18, 2013 at 11:30
  • Likewise, closing the tab and reopening the page in another tab works just fine. Commented Aug 18, 2013 at 14:16

1 Answer 1

1

I had the same problem. Check your code to see whether this happens after events that are fired from the DOM and are unknown to Angular. If so, you need to add $scope.$apply(); after the get request in order to make it happen.

I'm fairly new to Angular so I'm not sure this is the best practice for using Angular, but it did work in my case.

See this similar question for a better explanation.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, I've been focusing on other projects lately so I haven't been able to check this out. I'll try it soon, though!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.