简体   繁体   中英

Angular $http get

I want to make an ajax call in angularJS, JSON is loading but the data is not populating.

    demoApp.controller('MainController', function($scope, GetData) {

        $scope.data = null;
        GetData.getDoctors(function(dataResponse) {
            $scope.data = dataResponse;
        });

    });


    demoApp.factory('GetData', function($http) {

        var doctors = [];
        this.getDoctors = function(callBack) {
            $http({

                method: 'GET',
                url: 'json/location.json'
            }).
            success(function(data) {
                callBack(data);
            }).
            error(function(data) {
                alert("Error");
            });

        }
    });

Even the Json file is not loading ..what is the error in the code?

Angular factory should return something.

Try

demoApp.factory('GetData', function($http) {
    var methods = {};

    methods.getDoctors = function(callBack) {
        $http({
            method: 'GET',
            url: 'json/location.json'
        }).
        success(function(data) {
            callBack(data);
        }).
        error(function(data) {
            alert("Error");
        });
    };

    return methods;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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