duandan5471 2017-04-20 06:30
浏览 58

如何从服务器获取php Web api json的数据到使用angularJs。

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条回答 默认 最新

  • drfu29983 2017-04-20 07:25
    关注

    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 ????

    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果