2017-04-20 136 views
0

当我将这个json保存在本地计算机中并在我的angularjs脚本中使用它时。它工作正常。但是,当我使用直接服务器,这不是为什么。如何从服务器获取php Web api json的数据到使用angularJs?

我的脚本中缺少什么?

我的JSON文件>

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 
    }); 
}]) 

})();

如何使用这个json文件任何人请给我建议。

+0

你是否在控制台中看到数据? – Sajeetharan

+0

未显示 有错误 angular.min.js:102错误:[ng:areq] http://errors.angularjs.org/1.3.14/ng/areq?p0=axajCtrl&p1=not%20a %20function%2C%20got%20undefined – user1727852

+0

即zip文件 http://webstrokes.in/json-error.zip plz sir帮助我。 – user1727852

回答

0

首先我希望This plunker会对你有所帮助。 正如我穿越起源问题我保存在一个JSON文件的响应,这样做,工作周围像

$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); 
     }) 

让我知道,如果你有这个

我用你的JSON任何疑问,只是显示的ID和使用ng-repeat的数据名称HERE ......让我知道您希望以何种方式在您的html中显示此内容,例如简单的数据,或者您想在下拉菜单中显示?

+0

这是正确的代码,但 我的问题是 如何获取数据使用在线json路径到我的另一个SPA。 – user1727852

+0

即日志注释 ------------------------ XMLHttpRequest无法加载http://deals.ownaroof.com/api/webservices/locations_list以.json。请求的资源上没有“Access-Control-Allow-Origin”标题。因此不允许Origin'http:// localhost'访问。 – user1727852

+0

那就是我在我的答案中提到的我越来越多的来源问题.....尝试结束,然后您的代码将正常工作 –

相关问题