对REST API的GET调用返回null

时间:2016-05-30 23:23:00

标签: angularjs api rest

我正在尝试进行GET调用以测试REST API,但它一直返回null,有什么我做错了吗?

在controller.js中进行调用

function ConsumerSearchCtrl($scope, BusinessCategoryService) {

    console.log(BusinessCategoryService);

}

enter image description here

127.0.0.1:8000/api/category/完美无缺

enter image description here

API的services.js中的代码

/**
 *
 */
function BusinessCategoryService(WenzeyAPI, $q) {

    var scope = this;

    scope.categories = categories;

    function categories() {
        var q = $q.defer();
        WenzeyAPI.get('http://127.0.0.1:8000/api/category/').then(function success (res) {
            q.resolve(res.data);
        }, function failure (err) {
            q.reject(err);
        })
        return q.promise;

    }

}



/**
 *
 */
function WenzeyAPI() {

    var scope = this,
        ip = "http://127.0.0.1:8000";

    scope.get = get;
    scope.post = post;

    function get(url, data) {

        data = data || {};

        var req = {
            method: 'GET',
            url: url,
            data: data
        }

        var q = $q.defer();
        $http(req).then(function success(response) {
            q.resolve(response);
        }, function failure(err) {
            q.reject(err);
        });
        return q.promise;
    }

    function post(url, data) {

        data = data || {};

        var req = {
            method: 'POST',
            url: url,
            data: data
        }

        var q = $q.defer();
        $http(req).then(function success(response) {
            q.resolve(response);
        }, function failure(err) {
            q.reject(err);
        });
        return q.promise;
    }

} 

1 个答案:

答案 0 :(得分:0)

删除WenzeyAPI并使用$http已解决此问题。

function BusinessCategoryService($http) {

    this.getAllData = function () {

        return $http({
            method: 'GET',
            url: 'http://127.0.0.1:8000/api/category/',
        });

    }

}