AngularJS $ HTTP不发送GET请求 [英] AngularJS $http not sending GET request

查看:100
本文介绍了AngularJS $ HTTP不发送GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个角JS应用我的工作,我使用的是服务,定期(在 $超时)使我的API GET请求一个URL (两个角应用程序和API正在从5000端口上提供本地主机)。

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).

由于某些原因,看来$ HTTP是不实际发送GET。对于每个 $ http.get() .error()被称为空数据和状态0当我在我的服务器日志查询(我运行一个Ruby on Rails的后端与我的服务器独角兽宝石),看来该服务器从未接收到来自角的要求。

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.

下面是我的服务功能:

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()

什么是真正奇怪的是,它只发生的有时的。当它停止工作,我可以更改 $ http.get()/ API / V1 / status.json,和它的作品。一段时间。然后我打开它回来了,它再次工作,一会儿......明明有在作怪一些更大的问题。

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.

我已经费尽我的大脑了几天,现在,我已经看到了一堆关于如此相似的问题,但他们似乎都与角实施CORS,我不认为要解决适用我的情况,因为这一切都从本地主机来:5000 。我错了吗?这是怎么回事?

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?

有关参考,我采用了棱角分明的版本1.0.7。

For reference, I'm using Angular version 1.0.7.

推荐答案

我有同样的问题。
检查你的code,看看是否可以从DOM解雇,是未知的角事件之后发生这种情况。
如果是这样,你需要添加 $ $范围适用于(); 之后才能做到这一点的GET请求

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.

请参阅此<一个href=\"http://stackoverflow.com/questions/17039998/angular-not-making-http-requests-immediately\">similar问题的一个更好的解释。

See this similar question for a better explanation.

这篇关于AngularJS $ HTTP不发送GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆