2017-04-20 133 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

那就是我在我的答案中提到的我越來越多的來源問題.....嘗試結束,然後您的代碼將正常工作 –

相關問題