angular $ http总是返回状态0

时间:2014-10-17 07:31:09

标签: angularjs

我使用angular $ http来调用webservice,但我无法从服务中获取任何数据,状态始终为0,

var httpurl = "http://127.0.0.1:3000?orderCode=700501898";


var myApp = angular.module('piechart', []);


myApp.factory('MyService', ['$http', function ($http) {
    return new function () {

        this.GetName = function () {
            return $http({
                url: httpurl,
                method:'GET'
            })
        };
    };
}]);
   angular.injector(['ng', 'piechart']).invoke(function (MyService) {

    MyService.GetName();

});

1 个答案:

答案 0 :(得分:1)

为什么要将$http服务封装两次?

var httpurl = "http://127.0.0.1:3000?orderCode=700501898";


var myApp = angular.module('piechart', []);


myApp.factory('MyService', ['$http', function ($http) {
    return {

        GetName:function () {
            return $http({
                url: httpurl,
                method:'GET'
            })
        }
    };
}]);

   angular.injector(['ng', 'piechart']).invoke(function (MyService) {

    MyService.GetName();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>