how to get data of php Web api json from server to using angularJs.?

六月ゝ 毕业季﹏ 提交于 2019-12-25 06:27:07

问题


When i am save this json in my local computer and use it in my angularjs script. It's work fine. But when i am using direct server to this it is not working why.?

What are missing in my script.?

my Json File >

http://deals.ownaroof.com/api/webservices/locations_list.json

Html >

<div ng-controller='prefferedCtrl'>{{locations}}</div>

Script >

(function(){
angular.module('myapp',['ngRoute'])
//afactory to consume webservices and return data to controllers.
.service('webServices',['$http',function($http){
    return {
        getLocations : function(){
            return  $http.get('http://deals.ownaroof.com/api/webservices/locations_list.json').then(function(response){ //wrap it inside another promise using then
                return response.data.response.locations;  //only return locations 
            });
        }
    }
}])
//define controller and inject webServices service as dependency.
.controller('prefferedCtrl',['webServices','$scope',function(webServices,$scope,$ngRoute){ 
    webServices.getLocations().then(function(response){ 
        $scope.locations = response; //Assign data received to $scope.data
    });
}]) 

})();

how to use this json file anyone please suggest me.


回答1:


First of all i am Hoping This plunker will be helpful to you. As i cross origin issues i save the response in a json file and did this work around like

$http.get('j1.json').success(function(response){ //make a get request to mock json file.
            $scope.data = response; //Assign data recieved to $scope.data
            console.log($scope.data);
        })

Let me know if you have any queries on this

I used your json and simply displayed id and name of your data using ng-repeat HERE......let me know in which way you want to show this in your html like simple data or you want to show then in a drop down ????



来源:https://stackoverflow.com/questions/43511846/how-to-get-data-of-php-web-api-json-from-server-to-using-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!