简体   繁体   English

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

[英]AngularJS $http returning 200 ok, but fail

I'm trying to do a simple ajax request, and the response status is "200, ok", using the Postman I can validate my JSON, and looks fine, and the header is ok too ... but $http aways fail. 我正在尝试做一个简单的ajax请求,响应状态为“200,ok”,使用Postman我可以验证我的JSON,看起来很好,标题也可以......但$ http aways失败。 I try to put a very simple JSON and aways have the same result. 我尝试放一个非常简单的JSON,并且具有相同的结果。 The solutions doesn't work for me 解决方案对我不起作用

My ANGUJAR JS request: 我的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);
            });

My JSON result, but with a simple response like [] I had the same problem : 我的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"}]

Tanks a lot. 坦克很多。

My problem was with CORS access. 我的问题是CORS访问。 the server don't allow requests from my AngularJS application ... 服务器不允许来自我的AngularJS应用程序的请求......

I enable my apache to allow access 我允许我的apache允许访问

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

In the file, put inside the tag 在文件中,放入标签内

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

And restart the apache 然后重启apache

See. 看到。 http://www.mechonomics.com/enabling-cors-cross-origin-resource-sharing-for-apache2-on-ubuntu-12-04/ http://www.mechonomics.com/enabling-cors-cross-origin-resource-sharing-for-apache2-on-ubuntu-12-04/

Tanks 坦克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM