AngularJS $ http返回200 ok,但失败了

时间:2016-04-26 21:03:11

标签: javascript angularjs json ajax

我正在尝试做一个简单的ajax请求,响应状态是“200,ok”,使用Postman我可以验证我的JSON,看起来很好,标题也可以......但是$ http客场失败。 我尝试放一个非常简单的JSON,并且具有相同的结果。 解决方案对我不起作用

我的ANGUJAR JS请求:

$http.get('http://127.0.0.1:8000/produto/')
            .then(function successCallback(response) {
                console.info('success =)');
                console.info(response);
                // this callback will be called asynchronouslywhen the response is available

            }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                console.info('fail =(');
                console.info(response);
            });

我的JSON结果,但是像[]这样的简单响应我遇到了同样的问题:

[{"id":1,"nome":"NOME UPD","doce":"1","deleted_at":null,"created_at":"2016-04-26 13:23:49","updated_at":"2016-04-26 13:23:49"},{"id":2,"nome":"NOME UPD","doce":"1","deleted_at":null,"created_at":"2016-04-26 13:24:25","updated_at":"2016-04-26 13:24:25"},{"id":3,"nome":"NOME UPD","doce":"1","deleted_at":null,"created_at":"2016-04-26 13:24:46","updated_at":"2016-04-26 13:24:46"},{"id":4,"nome":"NOME UPD","doce":"1","deleted_at":null,"created_at":"2016-04-26 13:25:16","updated_at":"2016-04-26 13:25:16"},{"id":5,"nome":"NOME UPD","doce":"1","deleted_at":null,"created_at":"2016-04-26 13:28:06","updated_at":"2016-04-26 13:28:06"},{"id":6,"nome":"NOME UPD","doce":"1","deleted_at":null,"created_at":"2016-04-26 18:05:15","updated_at":"2016-04-26 18:05:16"},{"id":7,"nome":"NOME UPD","doce":"1","deleted_at":null,"created_at":"2016-04-26 18:52:42","updated_at":"2016-04-26 18:52:43"},{"id":8,"nome":"NOME UPD","doce":"1","deleted_at":null,"created_at":"2016-04-26 20:29:43","updated_at":"2016-04-26 20:29:43"}]

坦克很多。

1 个答案:

答案 0 :(得分:0)

我的问题在于CORS访问权限。 服务器不允许来自我的AngularJS应用程序的请求......

我启用了apache以允许访问

a2enmod headers
sudo nano /etc/apache2/sites-available/default

在文件中,放入标签

Header set Access-Control-Allow-Origin "*"

然后重启apache

请参阅。 http://www.mechonomics.com/enabling-cors-cross-origin-resource-sharing-for-apache2-on-ubuntu-12-04/

坦克